我已经创建了一个带有选项菜单的活动,我试图根据视图分页器菜单中的片段更改来设置视觉效果,这是指示空指针异常
活动
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.message_user_list_menu, menu);
return true;
}
在我正在使用的片段中:
@SuppressLint("NewApi")
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
menu.findItem(R.id.refreshmenu_home_container).setVisible(false);
menu.findItem(R.id.new_msg_chat_home_container).setVisible(true);
menu.findItem(R.id.action_search_chat_home_container).setVisible(true);
menu.findItem(R.id.chat_menu_attach_home_container).setVisible(true);
}
堆栈跟踪:
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): java.lang.NullPointerException
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at com.rancore.messaging.ui.RTuserListFragment.onCreateOptionsMenu(RTuserListFragment.java:273)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1582)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1956)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1585)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1956)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:408)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:759)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.ActivityCompatHoneycomb.invalidateOptionsMenu(ActivityCompatHoneycomb.java:29)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.FragmentActivity.supportInvalidateOptionsMenu(FragmentActivity.java:648)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v7.app.ActionBarActivity.supportInvalidateOptionsMenu(ActionBarActivity.java:170)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1562)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.FragmentManagerImpl$3.run(FragmentManager.java:495)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.os.Handler.handleCallback(Handler.java:605)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.os.Looper.loop(Looper.java:137)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at android.app.ActivityThread.main(ActivityThread.java:4507)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-14 16:20:35.503 E/com.rancore.contactbook.RTContactBookActivity<======( 1340): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
这是因为您错过了扩充布局文件。所以改变
inflater.inflate(R.menu.message_user_list_menu, menu);
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
inflater.inflate(R.menu.message_user_list_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
menu.findItem(R.id.refreshmenu_home_container).setVisible(false);
menu.findItem(R.id.new_msg_chat_home_container).setVisible(true);
menu.findItem(R.id.action_search_chat_home_container).setVisible(true);
menu.findItem(R.id.chat_menu_attach_home_container).setVisible(true);
}