导航抽屉菜单不会打开活动

时间:2014-07-14 11:28:43

标签: android android-fragments navigation-drawer

我所拥有的是左导航菜单,我使用导航抽屉,使用碎片工作得很好但是当我试图改变它以使用活动时,它显示我没有错误的黑屏在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");
        }
    }

谁能说出问题是什么?

2 个答案:

答案 0 :(得分:0)

检查以下代码

 Intent i1; 

 private void displayView(int position) {
    // update the main content by replacing fragments
    //android.app.Fragment fragment = null;
    switch (position) {
    case 0:
        i1 =  new Intent(MainActivity.this, activity.Class);
        i1.putExtra("case 0", position);
        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) {
         mDrawerLayout.closeDrawer(mDrawerList);
        setTitle(navMenuTitles[position]);
         startActivity(i1);  


      }

    else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

答案 1 :(得分:0)

这很奇怪:

new MainActivity();

您不应该自己制作Activity个对象,这应该由ActivityManager完成。我会做以下事情:

Intent startIntent;
...
case 0:
        startIntent =  new Intent(MainActivity.this, SecondActivity.class);
        startIntent.putExtra("case 0", position);
        break;
...
if(intent != null) {
    startActivity(startIntent);  
}

顺便说一句,根据您的代码,您尝试启动相同的代码,已经在运行MainActivity