在我的app类中,ProjectFragActivtiy遇到了像
这样的异常java.lang.ClassCastException: com.actionbarsherlock.internal.view.menu.MenuBuilder
at com.oj.bs.ProjectFragActivity.onCreateOptionsMenu
(ProjectFragActivity.java:49).
应用菜单按钮中的用于注销操作但遇到异常。还有另一种方法可以在actionbarsherlock中实现菜单。 有人有解决方案吗?提前谢谢。
以下是ProjectFragActivtiy类
package com.oj.bs;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
public class ProjectFragActivity extends SherlockFragmentActivity {
SessionManager sessionMngr ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sessionMngr = new SessionManager(getApplicationContext());
Toast.makeText(getApplicationContext(), "User Login Status: " + sessionMngr .isLoggedIn(),
Toast.LENGTH_LONG).show();
sessionMngr.checkLogin();
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
Tab tab = actionBar.newTab().
setText("Residential").
setTabListener(new ResidentialFragActivity()).
setIcon(R.drawable.residential);
actionBar.addTab(tab);
tab = actionBar.newTab().
setText("Commercial").
setTabListener(new CommercialFragActivity()).
setIcon(R.drawable.commercial);
actionBar.addTab(tab);
}
public void Logout(View v) {
sessionMngr.logoutUser();
}
@Override
public boolean onCreateOptionsMenu(Menu m) {
getMenuInflater().inflate(R.menu.menu_logout, (android.view.Menu) m);
return true;
}
}
答案 0 :(得分:1)
按如下所示更改onCreateOptionsMenu()
@Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_logout, (com.actionbarsherlock.view.Menu) menu);
return true;
}
注意:
要从XML中扩充菜单,您应该在活动中调用getSupportMenuInflater()
。片段回调已经将inflater的一个实例作为参数。