我有什麼是左導航菜單,我使用導航抽屜,它工作得很好,但當我試圖改變它與活動,它顯示我一個黑色屏幕在logcat的沒有錯誤,這裏是我的代碼:導航抽屜菜單不會打開活動
public class MainActivity extends Activity {
Activity activity=null;
//on create and other things
//.
//.
//.
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
//android.app.Fragment fragment = null;
switch (position) {
case 0:
activity = new MainActivity();
break;
case 1:
//fragment = new FindPeopleFragment();
break;
case 2:
//fragment = new PhotosFragment();
break;
case 3:
//fragment = new CommunityFragment();
break;
case 4:
//fragment = new PagesFragment();
break;
case 5:
//fragment = new WhatsHotFragment();
break;
default:
break;
}
if(activity != null) {
Intent i1 = new Intent(MainActivity.this, activity.getClass());
i1.putExtra("case 0", position);
startActivity(i1);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
誰能告訴是什麼問題?
我覺得創建一個新的活動對象並不是你想要做的。我將刪除'activity = new MainActivity()',並將意圖創建和'startActivity()'移入case語句。確保您可以通過用'MainActivity.class'替換'activity.getClass()'來正常啓動活動。 –
我以前這樣做過,但它沒有工作 – sereen
我只有創建過活動「Intent intent = new Intent(this,MainActivity.class)''。你能發佈你之前嘗試過的代碼嗎? –