-5

I am trying to put a notification counter inside the action bar and it gives me this error inside the onCreateOptionsMenu method:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference
        at ro.oce.store.MainActivity.onCreateOptionsMenu(MainActivity.java:112)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2823)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:262)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:241)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
        at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:448)
        at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:65)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Here is the code:

 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.shopping_cart).getActionView();
    TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
    tv.setText("12");
    return true;
}

Before I added the last 3 lines of code, my application didn't generate any errors. Can somebody please help me?

Pikachuu
  • 145
  • 2
  • 5
  • 17

1 Answers1

0

Two suggestions:

  • Take away the "badgeLayout" when finding by id - (TextView)findViewById();

  • Check that the layout isn't null, the error "null object reference", means that the invoking instance is null. So in this case "badgeLayout" is null. Do the following:

    -> Check that the layout xml that you are referencing exists.

    -> remove the getActionView()

TejjD
  • 2,571
  • 1
  • 17
  • 37
  • I tried the second suggestion and it gave me this exeption `ClassCastException: android.support.v7.internal.view.menu.MenuItemImpl cannot be cast to android.widget.RelativeLayout`. The first suggestion gave me another exeption: `NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference` – Pikachuu Sep 17 '15 at 09:06