1

In my app I have a toolbar which is working perfectly fine in my MainActivity.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/toolbar_item_counter"
        android:id="@+id/toolbar_tv_itemCounter"
        android:textSize="18dp"
        android:textColor="#ffffff"
        android:textStyle="bold"/>
</RelativeLayout>

But when I try to inflate this menu

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

<item android:id="@+id/item_accept"
    android:title="Accept"
    android:icon="@drawable/ic_action_accept"
    app:showAsAction="always"/>
</menu>

With this code

public class AddActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.inflateMenu(R.menu.menu_input_accept);

    TextView toolbartext = (TextView) findViewById(R.id.toolbar_tv_itemCounter);
    toolbartext.setVisibility(View.INVISIBLE);

}

}

It gives me this

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.inflateMenu(int)' on a null object reference

fatal error which I don't understand. I also tried to overwrite the onCreateOptionsMenu method and use the MenuInflater there like I do in my MainActivity but it changes nothing. I included my toolbar in the xml like I did in my main activity:

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

user
  • 85,380
  • 17
  • 189
  • 186
creativecreatorormaybenot
  • 62,078
  • 29
  • 174
  • 277

1 Answers1

1

I included my toolbar in the xml like I did in my main activity:

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

When you include a layout, if you provide an id to the include tag(like you did, by using android:id="@+id/include") then the root of the included layout will have that id, overriding any id already assigned inside the included layout. So in your case the Toolbar has an id of R.id.include and not R.id.toolbar.

Either remove android:id="@+id/include", change it to android:id="@+id/toolbar" or look for the Toolbar using findViewById(R.id.include).

Community
  • 1
  • 1
user
  • 85,380
  • 17
  • 189
  • 186