0

I don't understand why in this case is not recongnizing my menu item.

I have another activities with similar code and it's working fine, but in this case i'm having NPE.

Layouts

General (activity_completas)

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.CompletasActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_breviario_general" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_breviario_general

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include
        android:id="@+id/include"
        layout="@layout/tv_zoomable" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        app:layout_constraintBottom_toTopOf="@+id/include"
        app:layout_constraintEnd_toStartOf="@+id/include"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

Menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.BreviarioActivity">

<item
    android:id="@+id/item_voz"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:icon="@drawable/ic_play"
    android:visible="false"
    android:title="@string/leer"
    app:showAsAction="ifRoom" />
<item
    android:id="@+id/item_calendario"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:icon="@drawable/ic_calendar_toolbar"
    android:title="@string/all_calendar"
    app:showAsAction="ifRoom" />
</menu>

Java code

Relevant parts only for this case:

public class CompletasActivity extends AppCompatActivity {
    //...
    private Menu menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_completas);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        showData();
    }

    private void showData() {
        sbReader = new StringBuilder();
        //I fill sbReader correctly
        if (sbReader.length()>0) {
            menu.findItem(R.id.item_voz).setVisible(true);  //******NPE here*******
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.menu = menu;
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                if (tts != null) {
                    tts.cerrar();
                }
                NavUtils.navigateUpFromSameTask(this);
                return true;

            case R.id.item_voz:
                String html = String.valueOf(Utils.fromHtml(sbReader.toString()));
                String[] textParts = html.split(SEPARADOR);
                tts = new TTS(getApplicationContext(), textParts);
                return true;

            case R.id.item_calendario:
                Intent i = new Intent(this, CalendarioActivity.class);
                startActivity(i);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (tts != null) {
            tts.cerrar();
        }
    }
}

Error

java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.findItem(int)' on a null object reference

I do the same thing in another activities and i don't have NEP. What's happening here?

A. Cedano
  • 557
  • 7
  • 39
  • 1
    Looks like onCreateOptionsMenu will be called after onCreate. Since you are initializing variable menu inside onCreateOptionsMenu, and trying to access it in onCreate, it is null. https://stackoverflow.com/questions/22340598/what-is-the-call-order-of-oncreateoptionsmenu-in-the-android-activity-lifecycl – Bob Dec 12 '19 at 11:52

1 Answers1

1

You are getting null point exception because your menu isn't instantiated. As i can see, your are calling showData before creating the menu. Call showData in the onCreateMenu. This should solve your problem.

Denis95
  • 136
  • 3
  • Ok, it works!. But why in another cases i'm not having NPE? In another cases in `showData()` I launch one Firestore request or one Volley request, and here i get the data from a raw file (locally). – A. Cedano Dec 12 '19 at 12:03
  • It means the menu gets instantiated before the showData method call. – Denis95 Dec 12 '19 at 12:14