我想用包含4個片段的MenuDrawer製作一個應用程序。 這是我的應用程序顯示的功能。 當它啓動時,它應該從網站檢索數據。檢索到的數據應該位於第一個片段的列表視圖中。 第二個片段應該包含一個webview來查看我的android設備中的頁面。 三是顯示一些文字細節。 當第一個片段從網站獲取詳細信息時,我的應用程序崩潰。這裏是我的代碼: 我的片段佈局如下:處理片段和listView和JSON
我的第一個片段包含以下內容:
<ListView
android:layout_marginTop="10dp"
android:layout_below="@+id/txtLabel"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background = "@android:color/transparent"
android:padding="3dp"
android:dividerHeight="1dp"
android:id="@+id/mlv"></ListView>
第一個片段稱爲應用程序啓動時。 我有一個列表適配器和列表模型運行良好。
我的主類包含一個獲取數據的內部類。 我已經做了我的模型類和適配器類: 這是我的主要代碼:
public class MainActivity extends Activity {
// declare properties
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ListView mListView;
private ListViewNewsAdapter listViewNewsAdapter;
private ArrayList<ListViewNewsItem> listViewNewsItems;
private JSONParser jsonParser = new JSONParser();
private String READNEWS_URL =
"xyz-myweb-xyz.com";
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.mlv);
// for proper titles
mTitle = mDrawerTitle = getTitle();
// initialize properties
mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// list the drawer items
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[4];
drawerItem[0] = new ObjectDrawerItem(R.drawable.home_icon, "Home");
drawerItem[1] = new ObjectDrawerItem(R.drawable.website_icon, "Website");
drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_share, "Help");
// Pass the folderData to our ListView adapter
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
// Set the adapter for the list view
mDrawerList.setAdapter(adapter);
// set the item click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// for app icon control for nav drawer
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
if (savedInstanceState == null) {
// on first time display view for first nav item
selectItem(0);
}
}
這與一些碎片的方法,它的myMainActivity內:
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new CreateFragment();
new GetNewsTask().execute();
break;
case 1:
fragment = new ReadFragment();
break;
case 2:
fragment = new HelpFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainAct10ity", "Error in creating fragment");
}
}
我所使用的內部類從網上檢索數據:
private class GetNewsTask extends AsyncTask<Void, Void, Boolean>
{
private ProgressDialog mProgressDialog;
private JSONObject jsonObjectResult = null;
private String error;
@Override
protected void onPreExecute()
{
super.onPreExecute();
listViewNewsItems = new ArrayList<ListViewNewsItem>();
mProgressDialog = ProgressDialog.show(MainActivity.this,
"Processing...", "Get last news", false, false);
}
@Override
protected Boolean doInBackground(Void... params)
{
jsonObjectResult = jsonParser.makeHttpRequest(READNEWS_URL, null);
if (jsonObjectResult == null)
{
error = "Error in the connection";
return false;
}
try
{
if (jsonObjectResult.getInt("success") == 1)
{
JSONArray jsonArray = jsonObjectResult.getJSONArray("posts");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject news = jsonArray.getJSONObject(i);
ListViewNewsItem listViewNewsItem = new ListViewNewsItem
(
news.getString("title"),
news.getString("type")
);
listViewNewsItems.add(listViewNewsItem);
}
return true;
}
else
error = jsonObjectResult.getString("message");
}
catch (Exception ex)
{
}
return false;
}
@Override
protected void onPostExecute(Boolean aBoolean)
{
super.onPostExecute(aBoolean);
mProgressDialog.dismiss();
if (aBoolean)
{
listViewNewsAdapter = new ListViewNewsAdapter(getApplicationContext(),
listViewNewsItems);
mListView.setAdapter(listViewNewsAdapter);
}
else
Toast.makeText(getApplicationContext(), error, Toast.LENGTH_LONG).show();
}
}
我在哪裏必須使用
new GetNewsTask().execute();
我試圖在onCreate中使用它,我的應用程序崩潰。我試圖在交換機內使用它,我的應用崩潰了。 你有什麼想法如何解決它?
如果你把它放在你的片段的onCreate你得到什麼? –
我還沒有嘗試過。但是,我現在試一試 – ama989
,它必須在片段上,而不是在主要活動上的列表 –