我在我的项目中使用了actionbarsherlock,我正试图从片段中操纵动作栏......
所以在我显示的第一个片段中,我重写onPrepareOptionsMenu以使操作栏无效并更改标题...但是当我尝试调用supportInvalidateOptionsMenu()时,我得到一个java.lang.StackOverflowError(它在android 4.3上不会发生)...
我已经尝试Delyan's solution但没有成功...我的代码和日志如下,有什么建议吗?提前致谢
--------- ---------代码
@Override
public void onPrepareOptionsMenu(Menu menu){
super.onPrepareOptionsMenu(menu);
Log.e("CategoriasLista", "Executou onPrepareOptionsMenu");
int currentTab = ((MainActivity)getSherlockActivity()).getTabHost().getCurrentTab();
if(currentTab==0){
ActionBar bar = getSherlockActivity().getSupportActionBar();
//getSherlockActivity().supportInvalidateOptionsMenu();
((MainActivity)getSherlockActivity()).runOnUiThread(new Runnable(){
@Override
public void run(){
((MainActivity)getSherlockActivity()).supportInvalidateOptionsMenu();
}
});
bar.setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_STANDARD);
bar.setTitle("Categorias");
}
}
---------登录-----------
10-21 09:35:32.910: ERROR/AndroidRuntime(5231): FATAL EXCEPTION: main
java.lang.StackOverflowError
at java.lang.Class.getClassLoaderImpl(Class.java:431)
at java.lang.Class.getClassLoader(Class.java:398)
at android.os.Bundle.<init>(Bundle.java:68)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:251)
at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:149)
at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:155)
at br.com.maisapp.Categorias.CategoriasLista$1.run(CategoriasLista.java:60)
at android.app.Activity.runOnUiThread(Activity.java:3743)
at br.com.maisapp.Categorias.CategoriasLista.onPrepareOptionsMenu(CategoriasLista.java:57)
at android.support.v4.app.Watson.onPreparePanel(Watson.java:98)
at com.actionbarsherlock.ActionBarSherlock.callbackPrepareOptionsMenu(ActionBarSherlock.java:581)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:494)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:265)
at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:149)
at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:155)
at br.com.maisapp.Categorias.CategoriasLista$1.run(CategoriasLista.java:60)
at android.app.Activity.runOnUiThread(Activity.java:3743)
at br.com.maisapp.Categorias.CategoriasLista.onPrepareOptionsMenu(CategoriasLista.java:57)
at android.support.v4.app.Watson.onPreparePanel(Watson.java:98)
at com.actionbarsherlock.ActionBarSherlock.callbackPrepareOptionsMenu(ActionBarSherlock.java:581)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:494)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:265)
at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:149)
at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:155)
at br.com.maisapp.Categorias.CategoriasLista$1.run(CategoriasLista.java:60)
at android.app.Activity.runOnUiThread(Activity.java:3743)
at br.com.maisapp.Categorias.CategoriasLista.onPrepareOptionsMenu(CategoriasLista.java:57)
at android.support.v4.app.Watson.onPreparePanel(Watson.java:98)
at com.actionbarsherlock.ActionBarSherlock.callbackPrepareOptionsMenu(ActionBarSherlock.java:581)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:494)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:265)
at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:149)
at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:155)
at br.com.maisapp.Categorias.CategoriasLista$1.run(CategoriasLista.java:60)
at android.app.Activity.runOnUiThread(Activity.java:3743)
at br.com.maisapp.Categorias.CategoriasLista.onPrepareOptionsMenu(CategoriasLista.java:57)
at android.support.v4.app.Watson.onPreparePanel(Watson.java:98)
at com.actionbarsherlock.ActionBarSherlock.callbackPrepareOptionsMenu(ActionBarSherlock.java:581)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:494)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:265)
at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:149)
at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:155)
at br.com.maisapp.Categorias.CategoriasLista$1.run(CategoriasLista.java:60)
at android.app.Ac
答案 0 :(得分:2)
最有可能抛出异常,因为你试图在错误的地方使它失效。
如果您只想更改标题,请将逻辑移至onActivityCreated方法。
@Override
protected void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getSherlockActivity.setTitle("Categorias");
}