When I test my app on Xiaomi A2, it runs fine. When I upload to Google Play, it crashes. Stack Trace on Play console reads:
java.lang.NullPointerException:
at com.thongjoon.ocsc_exam_prep.MainMenu.onCreateOptionsMenu (MainMenu.java:29)
at android.app.Activity.onCreatePanelMenu (Activity.java:4140)
at androidx.fragment.app.FragmentActivity.onCreatePanelMenu (FragmentActivity.java:2) ...
This happens after I migrate to AndroidX.
Here is my onCreateOptionsMenu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_welcome, menu);
MenuItem item = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
mShareActionProvider.setShareIntent(getDefaultShareIntent());
String isAvailable = getApplicationContext().getResources().getString(R.string.is_show_order);
MenuItem buyItem = menu.findItem(R.id.action_buy_sheets);
// if (isAvailable!=null) {
if (!isAvailable.equals("yes")) {
buyItem.setVisible(false);
} else {
buyItem.setVisible(true);
}
// }
return true;
}
and
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
...
if (id == R.id.action_buy_sheets) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://forms.gle/1PYvktbuGPM3MScZ9")));
return true;
}
...
}
in build.gradle
...
dependencies {
implementation fileTree(include: ['*jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
}
...
I use Analyze>Inspect Code to find error. No error reported. I am new. This app is my first app. Please help. Very much appreciated.