2014-07-14 117 views
0

我有什麼是左導航菜單,我使用導航抽屜,它工作得很好,但當我試圖改變它與活動,它顯示我一個黑色屏幕在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"); 
     } 
    } 

誰能告訴是什麼問題?

+0

我覺得創建一個新的活動對象並不是你想要做的。我將刪除'activity = new MainActivity()',並將意圖創建和'startActivity()'移入case語句。確保您可以通過用'MainActivity.class'替換'activity.getClass()'來正常啓動活動。 –

+0

我以前這樣做過,但它沒有工作 – sereen

+0

我只有創建過活動「Intent intent = new Intent(this,MainActivity.class)''。你能發佈你之前嘗試過的代碼嗎? –

回答

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

+0

好吧,我已經嘗試了你的代碼..我把另一個活動的祕密活動打開它的自我沒有點擊按鈕的奇怪的事情。爲什麼發生這種情況? – sereen

+0

這是因爲創建抽屜時會自動調用「情況0:」。在設置抽屜式導航器後,在onCreate()方法調用'displayView(default_position)'中,其中'default_position'是您希望在啓動時調用的情況。 –

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"); 
    } 
}