2012-10-19 59 views
0

這是SherlockActionBar示例,它使用靜態附件,我運行到ICE_CREAM_SANDWICH設備,但方法onOptionsItemSelected未被調用!onOptionsItemSelected在使用設備API級別14(SherlockActionBar)時不調用(SherlockActionBar)

代碼的多餘部分被刪除,3.x和2.x的版本試過沒有問題,幫幫我~~

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    setTheme(SampleList.THEME); // Used for theme switching in samples 
    super.onCreate(savedInstanceState); 

    mSherlock 
      .setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW); 
    mSherlock.setContentView(R.layout.text); 

    ((TextView) findViewById(R.id.text)) 
      .setText(R.string.static_attach_content); 
} 

@Override 
public boolean onCreateOptionsMenu(android.view.Menu menu) { 
    return mSherlock.dispatchCreateOptionsMenu(menu); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Used to put dark icons on light action bar 
    boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light; 

    menu.add("Save")... 

    menu.add("Search")... 

    menu.add("Refresh")... 

    return true; 
} 

@Override 
public boolean dispatchKeyEvent(KeyEvent event) { 
    if (mSherlock.dispatchKeyEvent(event)) { 
     return true; 
    } 
    return super.dispatchKeyEvent(event); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    Log.i("", "on options item selected!!!!!!!!!!!!!!!!"); 
    Toast.makeText(this, item.toString(), Toast.LENGTH_SHORT).show(); 
    return true; 
} 

} 
+0

你自定義視圖添加到您的菜單按鈕? –

+0

@TomislavNovoselec這個樣本沒有添加。 – zkwlx

+0

哦!!我發現!!我忘了覆蓋Activity.onOptionsItemSelected !!!! haha​​ha – zkwlx

回答

3

如果您使用的片段和片段和活動呼籲onOptionsItemSelected記得在活動調用返回super.onOptionsItemSelected(item);

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    // If home icon is clicked return to main Activity 
    case android.R.id.home: 
     getSupportFragmentManager().popBackStack(null, 
       FragmentManager.POP_BACK_STACK_INCLUSIVE); 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
+1

我找到原因,我忘記覆蓋onOptionsItemSelected,併發送到sherlock ......但謝謝你! – zkwlx

+1

謝謝,這似乎是使用碎片時唯一的工作解決方案。 – Nickolaus

相關問題