i am using a ShareActionProvider to create a share button. The code works fine if i run it in debug mode, but as soon as i change the build variant to release, it crashes throwing a NPE. Here is the code which crashes in the last line:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.jobdetail_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
shareActionProvider.setShareIntent(createShareIntent());
...
}
I already tested to change out the createShareIntent() to a simple new Intent, it does not matter. The Problem occurs since i updated all my dependencies and migrated the app to androidx. Here is the crashlog:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.widget.r0.a(android.content.Intent)' on a null object reference
at de.jobware.jana.activity.JobDetailActivity.onCreateOptionsMenu(JobDetailActivity.java:386)
at android.app.Activity.onCreatePanelMenu(Activity.java:3680)
at androidx.fragment.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:325)
at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2830)
at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
at androidx.appcompat.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:455)
at androidx.appcompat.app.ToolbarActionBar$1.run(ToolbarActionBar.java:56)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Edit: I found another issue: support.v7 ShareActionProvider crashes if showAsAction="always" , stating that this is caused by the XML file, which looks the same in my project:
<item
android:id="@+id/menu_item_share"
app:showAsAction="ifRoom"
android:title="@string/share"
app:actionProviderClass=
"androidx.appcompat.widget.ShareActionProvider" />
But since i am using androidx now, i cannot change the app:showAsAction to android:showAsAction, since it does not work with appcompat.. Edit 2: Reverting the migration to Androidx (and the dependencies) the code runs again, but thats not really solving the issue Thanks in advance for taking the time!