0

I'm using a menu to use two button on AppCompat ActionBar, like in the SearchView sample, that I have used on another class in this exact way, but it is giving me this error:

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)

Here is my menu 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>

and the code where I'm trying to use it:

@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;
    }

Someone knows why it is happening?

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
Daniel Nazareth
  • 530
  • 1
  • 6
  • 22
  • 1
    which line is 133 in ActivityFollowings? – peshkira Feb 18 '14 at 15:06
  • line 133 is btnFollowing.setText(...); that is crashing because btnFollowing is null – Daniel Nazareth Feb 18 '14 at 15:10
  • yes, that was what I thought. Could it be, the you inflate a different menu file. In your code listings you have written, the the menu file is ```menu.xml``` and in the code you have ```menu_switch.xml```. Is this correct? – peshkira Feb 18 '14 at 15:12
  • also, this might help: http://stackoverflow.com/questions/18438890/menuitemcompat-getactionview-always-returns-null – peshkira Feb 18 '14 at 15:13
  • This menu xml is the menu_switch.xml that I use in code. I've tried to change where was android:id... to app:id... but still giving me this error – Daniel Nazareth Feb 18 '14 at 15:22

0 Answers0