Alex Moiseenkov Alex Moiseenkov - 1 year ago 105
Android Question

NullPointerException when change Activity to AppCompatActivity

I changed my class from the Activity to AppCompatActivity.

And changed


Now I get NPE in the following code.

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

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

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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download