What cause "java.lang.NullPointerException" error when I run Notifications app in user-interface-samples project?

Viewed 323

The Code A is a sample code about Notifications in the project user-interface-samples.

I trun off the notifications of the app, then run the app, an information "You need to enable notifications for this app" is displayed.

When I click the Snackbar on API 28 emulator, the following error is displayed:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.settings/com.android.settings.Settings$AppNotificationSettingsActivity}: java.lang.NullPointerException: Attempt to read from field 'android.content.pm.ApplicationInfo android.content.pm.PackageInfo.applicationInfo' on a null object reference

When I click the Snackbar on API 30 emulator , the prompt message is displayed, see Image A.

The app wasn't found in the list of installed app.

What are problems with the sample code?

Code A

mNotificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());    

boolean areNotificationsEnabled = mNotificationManagerCompat.areNotificationsEnabled();
if (!areNotificationsEnabled) {
        // Because the user took an action to create a notification, we create a prompt to let
        // the user re-enable notifications for this application again.
        Snackbar snackbar = Snackbar
                .make(
                        mMainRelativeLayout,
                        "You need to enable notifications for this app",
                        Snackbar.LENGTH_LONG)
                .setAction("ENABLE", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // Links to this app's notification settings
                        openNotificationSettingsForApp();
                    }
                });
        snackbar.show();
        return;
 }


private void openNotificationSettingsForApp() {
    // Links to this app's notification settings.
    Intent intent = new Intent();
    intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
    intent.putExtra("app_package", getPackageName());
    intent.putExtra("app_uid", getApplicationInfo().uid);
    startActivity(intent);
}

Image A

enter image description here

2 Answers

update openNotificationSettingsForApp() this method, there is different code to open notification settings activity for Oreo and above!

private void openNotificationSettingsForApp() {
    // Links to this app's notification settings.

    Intent intent = new Intent();
    intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");

    //for Android 5-7
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
        intent.putExtra("app_package", getPackageName());
        intent.putExtra("app_uid", getApplicationInfo().uid);
    }
    // for Android 8 and above
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
    }
    startActivity(intent);
}

Try this

 mNotificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());    
    
    boolean areNotificationsEnabled = mNotificationManagerCompat.areNotificationsEnabled();
    if (!areNotificationsEnabled) {
            // Because the user took an action to create a notification, we create a prompt to let
            // the user re-enable notifications for this application again.
            Snackbar snackbar = Snackbar
                    .make(
                            mMainRelativeLayout,
                            "You need to enable notifications for this app",
                            Snackbar.LENGTH_LONG)
                    .setAction("ENABLE", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            // Links to this app's notification settings
                            openNotificationSettingsForApp();
                            snackbar.show(); // To here
                        }
                    });
           //cut from here 
            return;
     }
    
    
    private void openNotificationSettingsForApp() {
        // Links to this app's notification settings.
        Intent intent = new Intent();
        intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
        intent.putExtra("app_package", getPackageName());
        intent.putExtra("app_uid", getApplicationInfo().uid);
        startActivity(intent);
    }
Related