我正在使用菜单在AppCompat ActionBar上使用两个按钮,就像在SearchView示例中一样,我已经在另一个类中以这种方式使用了它,但它给了我这个错误:
02-18 11:52:06.644: E/AndroidRuntime(15988): FATAL EXCEPTION: main
02-18 11:52:06.644: E/AndroidRuntime(15988): java.lang.NullPointerException
02-18 11:52:06.644: E/AndroidRuntime(15988): at .activities.ActivityFollowings.onCreateOptionsMenu(ActivityFollowings.java:133)
02-18 11:52:06.644: E/AndroidRuntime(15988): at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
02-18 11:52:06.644: E/AndroidRuntime(15988): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
02-18 11:52:06.644: E/AndroidRuntime(15988): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
02-18 11:52:06.644: E/AndroidRuntime(15988): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
02-18 11:52:06.644: E/AndroidRuntime(15988): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-18 11:52:06.644: E/AndroidRuntime(15988): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-18 11:52:06.644: E/AndroidRuntime(15988): at android.view.Choreographer.doFrame(Choreographer.java:543)
02-18 11:52:06.644: E/AndroidRuntime(15988): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-18 11:52:06.644: E/AndroidRuntime(15988): at android.os.Handler.handleCallback(Handler.java:733)
02-18 11:52:06.644: E/AndroidRuntime(15988): at android.os.Handler.dispatchMessage(Handler.java:95)
02-18 11:52:06.644: E/AndroidRuntime(15988): at android.os.Looper.loop(Looper.java:136)
02-18 11:52:06.644: E/AndroidRuntime(15988): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-18 11:52:06.644: E/AndroidRuntime(15988): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 11:52:06.644: E/AndroidRuntime(15988): at java.lang.reflect.Method.invoke(Method.java:515)
02-18 11:52:06.644: E/AndroidRuntime(15988): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-18 11:52:06.644: E/AndroidRuntime(15988): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-18 11:52:06.644: E/AndroidRuntime(15988): at dalvik.system.NativeStart.main(Native Method)
这是我的菜单xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/menu_button_followers"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
app:actionViewClass="android.widget.Button"
app:showAsAction="always" />
<item
android:id="@+id/menu_button_following"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
app:actionViewClass="android.widget.Button"
app:showAsAction="always" />
</menu>
以及我正在尝试使用它的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_switch, menu);
MenuItem Following = menu.findItem(R.id.menu_button_following);
btnFollowing = (Button) MenuItemCompat.getActionView(Following);
MenuItem Followers = menu.findItem(R.id.menu_button_followers);
btnFollowers = (Button) MenuItemCompat.getActionView(Followers);
btnFollowing.setText(getResources().getString(R.string.txt_header_seguindo));
btnFollowers.setText(getResources().getString(R.string.txt_header_seguidores));
btnFollowing.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//onClick action
}
});
btnFollowers.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//onClick action
}
});
return true;
}
有人知道为什么会这样吗?