0

EDIT: Problem solved after upgrading Build Tools Version from 23 to 27.

I have the following code snippet:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.findItem(R.id.shareMenuItem).getActionView().setOnClickListener(onShareMenuItemClickedListener);
}

Recently I have got some errors on Crashlytics with line menu.findItem(...):

Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' on a null object reference
   at pl.application.ProductViewFragment.onCreateOptionsMenu(SourceFile:434)
   at android.support.v4.app.Fragment.performCreateOptionsMenu(SourceFile:2186)
   at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(SourceFile:2250)
   at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(SourceFile:328)
   at android.support.v4.app.FragmentActivity.onCreatePanelMenu(SourceFile:363)

The problem is not deterministic and shows only on Android Oreo (by Fabric - 100% of crashes were on Android 8.0, different devices). I have never had problems with this line before. Were there any important changes in Android 8.0 able to cause NPE there? I've tried reproduce it on my Xiaomi Mi A1, but with no effects. Or maybe there is a workaround?

Thanks!

// Edit: added xml menu file

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

<item
    android:id="@+id/breadcrumbsMenuItem"
    android:icon="@drawable/ic_breadcrumbs"
    android:title="@string/breadcrumbs"
    res:showAsAction="always" />

<item
    android:id="@+id/shareMenuItem"
    android:icon="@drawable/ic_menu_share"
    android:title="@string/share"
    res:actionLayout="@layout/layout_share_button"
    res:showAsAction="always" />

</menu>
Cililing
  • 4,303
  • 1
  • 17
  • 35

2 Answers2

0

Just try to add something like this, to see if it will be still null or not

Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
    public void run()
    {
        menu.findItem(R.id.shareMenuItem).getActionView().setOnClickListener(onShareMenuItemClickedListener);
    }
}, 2000);

If this fixes the problem, then the reason for him to be null is that is still hasn't been created and you area all rdy trying to access to it

I did have a problem when I was trying to change the color of an icon of my Menu acording to some info that it get's when the program starts, and that "work arround" fix the problem to me

Camadas
  • 509
  • 1
  • 5
  • 21
  • Like I told, I am not able to duplicate this error (also on crashing-devices). I guess maybe your answer is some kind of walkaround, but it doesn't explain why crash in only on Oreo. – Cililing Apr 10 '18 at 11:24
  • The only explanation that I could think is that when it reach to the part of the code `menu.findItem(R.id.shareMenuItem).getActionView().setOnClickListener(onShareMenuItemClickedListener);` is not created, I did have that same problem on Oreo also, all other versions did work at 100%, for the Oreo did have to use the work arround for it, the reason I rly don't know – Camadas Apr 10 '18 at 11:42
  • Actually we deployed this change about two weeks ago in our project. Does't work, bug occurs with the same frequency. – Cililing May 28 '18 at 07:56
0

I'm not sure where you get res:... but i think it should be app:...

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

<item
    android:id="@+id/breadcrumbsMenuItem"
    android:icon="@drawable/ic_breadcrumbs"
    android:title="@string/breadcrumbs"
    app:showAsAction="always" />

<item
    android:id="@+id/shareMenuItem"
    android:icon="@drawable/ic_menu_share"
    android:title="@string/share"
    app:actionLayout="@layout/layout_share_button"
    app:showAsAction="always" />

</menu>
Samuel Eminet
  • 4,647
  • 2
  • 18
  • 32
  • What's the difference? I mean why this could create an NPE, why only on Oreo, and why error is not deterministic. – Cililing Apr 10 '18 at 11:27