使用库slidingmenu和actionbarsherlock我尝试编写代码以使操作栏中的图标可单击,但我在崩溃时出错。这是切换代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
menu.toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
logcat说:
10-01 15:18:00.134: E/AndroidRuntime(27038): FATAL EXCEPTION: main
10-01 15:18:00.134: E/AndroidRuntime(27038): java.lang.NullPointerException
10-01 15:18:00.134: E/AndroidRuntime(27038): at com.db.batt.MainActivity.onOptionsItemSelected(MainActivity.java:512)
10-01 15:18:00.134: E/AndroidRuntime(27038): at android.support.v4.app.Watson.onMenuItemSelected(Watson.java:118)
10-01 15:18:00.134: E/AndroidRuntime(27038): at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:604)
10-01 15:18:00.134: E/AndroidRuntime(27038): at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchOptionsItemSelected(ActionBarSherlockNative.java:92)
10-01 15:18:00.134: E/AndroidRuntime(27038): at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:204)
10-01 15:18:00.134: E/AndroidRuntime(27038): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:171)
10-01 15:18:00.134: E/AndroidRuntime(27038): at android.view.View.performClick(View.java:4247)
10-01 15:18:00.134: E/AndroidRuntime(27038): at android.view.View$PerformClick.run(View.java:17733)
10-01 15:18:00.134: E/AndroidRuntime(27038): at android.os.Handler.handleCallback(Handler.java:730)
10-01 15:18:00.134: E/AndroidRuntime(27038): at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 15:18:00.134: E/AndroidRuntime(27038): at android.os.Looper.loop(Looper.java:137)
10-01 15:18:00.134: E/AndroidRuntime(27038): at android.app.ActivityThread.main(ActivityThread.java:5323)
10-01 15:18:00.134: E/AndroidRuntime(27038): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 15:18:00.134: E/AndroidRuntime(27038): at java.lang.reflect.Method.invoke(Method.java:525)
10-01 15:18:00.134: E/AndroidRuntime(27038): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:743)
10-01 15:18:00.134: E/AndroidRuntime(27038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
10-01 15:18:00.134: E/AndroidRuntime(27038): at dalvik.system.NativeStart.main(Native Method)
我该如何解决?
编辑: 我onCreate中的滑动菜单
SlidingMenu menu;
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidth(50);
menu.setFadeDegree(0.30f);
menu.setSlidingEnabled(true);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setBehindWidth(420);
menu.setMenu(R.layout.menu_frame);
//getActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
所以menu
声明不是吗?
答案 0 :(得分:2)
我敢打赌,你没有初始化menu
变量,这一行.-
menu.toggle();
抛出NullPointerException
。
修改强>
您在menu
中声明了一个局部变量onCreate
,但尝试访问onOptionsItemSelected
中的实例变量。删除该行.-
SlidingMenu menu;