-1

I changed my class from the Activity to AppCompatActivity.

And changed item.getActionView() to MenuItemCompat.getActionView(item)

Now I get NPE in the following code.

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.messages, menu);

    MenuItem item = menu.findItem(R.id.menuTxtSize);
    Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    int index = sp.getInt(getString(R.string.pr_text_size),
            Integer.parseInt(getString(R.string.pr_default_text_size)));
    spinner.setSelection(index);


E/ACRA: ACRA caught a NullPointerException for com.alex.documentation java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setSelection(int)' on a null object reference at com.alex.message.activities.MessagesActivity

How to fix the error?

  • 3
    Please include the stack trace and the actual line the NPE would be occuring on. – Kiskae Nov 29 '16 at 20:36
  • @Kiskae I added stack trace in a question – Alex Moiseenkov Nov 29 '16 at 20:48
  • @Rotwang Remove this as being marked as a duplicate. You referenced a simple question where someone is asking what an NPE is. This isn't a generic NPE question. – Jon Tinsman Feb 25 '17 at 07:23
  • @JonTinsman All NPEs are the same old story... If you know what an NPE is, you also know how to remove it. – Phantômaxx Feb 25 '17 at 09:40
  • @Rotwang I don't know who put you on the high horse you are on, but not all NPE exceptions are the same. If the person writing the code is modifying an object they created then yes, it is a simple NPE; however, the OP had working code and then when they tried to convert to an AppCompatActivity, the code no longer worked and it is throwing an NPE. As you can see from the accepted answer, it was not a simple "what is an NPE" but it had to do with the AndroidManifest.xml file needing to be modified. – Jon Tinsman Feb 27 '17 at 19:55
  • @JonTinsman What is not clear in "Null Pointer Exception"? Of course it's a non instanced object which has been used. How is that different in what the accepted answer says? – Phantômaxx Feb 27 '17 at 19:59
  • @Rotwang SO post [marking duplicates](http://meta.stackexchange.com/questions/230865/increase-close-vote-weight-for-gold-tag-badge-holders/231212#231212) says `Remember: duplicates are questions that ask for a solution to fundamentally identical problems - many questions have similar or identical answers but are not duplicates. By the same token, many questions are asked using very different wordings but seek to solve identical questions - closing these helps folks find their way to a solution even when they don't know what terms to search for.` This question can't be answered by your link – Jon Tinsman Feb 27 '17 at 20:08
  • @JonTinsman This question actually **can** be answered by my link. **If** you know what a NPE is, you also know how to handle it. But feel free to disagree. – Phantômaxx Feb 27 '17 at 20:10

1 Answers1

4

You have to use app:actionViewClass, not android:actionViewClass when using AppCompatActivity.

ianhanniballake
  • 155,909
  • 19
  • 351
  • 362
  • Yes you are right. What's the difference between `app:****` and `android:******` ? – Alex Moiseenkov Nov 29 '16 at 20:57
  • 2
    `android:` is system provided, while `app:` is provided by your app and the libraries it contains. See the [full explanation of the `android:` vs `app:` namespace](http://stackoverflow.com/questions/26692233/what-is-the-app-android-xml-namespace/26692768#26692768) for more details. – ianhanniballake Nov 29 '16 at 20:59
  • Thank you so much for this. I was having issues when I merged two projects together (two team members started the same app in different repos). One was Activity while the other was AppCompatActivity that had the search action bar. – Jon Tinsman Feb 27 '17 at 19:59