2014-02-06 73 views
1

我試圖從OptionsMenu啓動一個活動,但它不啓動。爲什麼?Android StartActivity選項菜單

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // toggle nav drawer on selecting action bar app icon/title 
    if (mDrawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
    // Handle action bar actions click 
    switch (item.getItemId()) { 
    case R.id.action_settings: 
     return true; 
    case R.id.main: 
     Intent intent = null; 
     intent = new Intent(MainActivity.this, Impostazioni.class); 
     this.startActivity(intent); 
     return true; 

    default: 
     return super.onOptionsItemSelected(item); 
    } 


} 
+0

您是否收到任何錯誤?您是否在AndroidManifest中聲明瞭Impostazioni? – Melquiades

+1

在R.id.main的情況下添加斷點或日誌語句以查看它是否實際上正在碰撞 – Naveed

回答

0
// Delay is in milliseconds 
static final int DRAWER_DELAY = 200; 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    //open navigationDrawer 
    new Handler().postDelayed(openDrawerRunnable(), DRAWER_DELAY); 

    // Handle action bar actions click 
    switch (item.getItemId()) { 
    case R.id.action_settings: 
     return true; 
    case R.id.main: 
     Intent intent = null; 
     intent = new Intent(MainActivity.this, Impostazioni.class); 
     this.startActivity(intent); 
     return true; 

    default: 
     return super.onOptionsItemSelected(item); 
    } 

} 

方法打開抽屜式導航欄......

private Runnable openDrawerRunnable() { 
    return new Runnable() { 

     @Override 
     public void run() { 
      mDrawerToggle.openDrawer(Gravity.LEFT); 
     } 
    } 
} 
+0

不,這種方式沒有任何反應,並且您甚至不打開導航抽屜 – user3281737

+0

該代碼優先於所有其餘的這裏的迴應。所以你迴歸真實,這就是你將要得到的。不應該調用'mDrawerToggle.onOptionsItemSelected(item)'在'case android.R.id.home'或類似的東西下的開關內部嗎? – SDJMcHattie

2

當你這樣做的時候,你最終不會回來嗎?

if (mDrawerToggle.onOptionsItemSelected(item)) { 
    return true; 
} 

我從來沒有達到startActivity的代碼。如果情況並非如此,你確定菜單項有ID R.id.main?我想用這個方法的頂部的一個斷點來調試這段代碼 - 然後逐步查看被調用的和不被調用的。