I am getting Broadcast receiver error as an exception for zero point in android studio. I want to receive GCM notifications

I use GCM to receive notifications in my application. To do this, I create a registration identifier during login and registration. After logging in or registering, the process throws an error and throws a zero point exception for the onRecieve method.

This error occurs sometimes, and sometimes it does not occur.

Here is the logarithm of the error.

01-15 12:26:02.935 22448-22448/com.simpler W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.BroadcastReceiver.onReceive(android.content.Context, android.content.Intent)' on a null object reference
01-15 12:26:02.936 22448-22448/com.simpler W/System.err:     at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
01-15 12:26:02.936 22448-22448/com.simpler W/System.err:     at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
01-15 12:26:02.936 22448-22448/com.simpler W/System.err:     at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
01-15 12:26:02.936 22448-22448/com.simpler W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:111)
01-15 12:26:02.936 22448-22448/com.simpler W/System.err:     at android.os.Looper.loop(Looper.java:194)
01-15 12:26:02.936 22448-22448/com.simpler W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5649)
01-15 12:26:02.936 22448-22448/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
01-15 12:26:02.936 22448-22448/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
01-15 12:26:02.936 22448-22448/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
01-15 12:26:02.936 22448-22448/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

Here is the code to generate the token at the input:

private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private static final String TAG1 = "SignInActivity";
private BroadcastReceiver mRegistrationBroadcastReceiver;

mRegistrationBroadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {

                    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
                    boolean sentToken = sharedPreferences
                            .getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
                    if (sentToken) {
                        System.out.println(getString(R.string.gcm_send_message));
                    } else {
                        System.out.println(getString(R.string.token_error_message));
                    }
                }
            };
if (checkPlayServices()) 
{
    // Start IntentService to register this application with GCM.
    Intent intent = new Intent(SignInActivity.this, RegistrationIntentService.class);
    startService(intent);
}

private boolean checkPlayServices() {
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (apiAvailability.isUserResolvableError(resultCode)) {
            apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                    .show();
        } else {
            Log.i(TAG1, "This device is not supported.");
            finish();
        }
        return false;
    }
    return true;
}
+4
source share
1 answer

To code

You must enable your local translator.
if (checkPlayServices()) {
// Start IntentService to register this application with GCM.
Intent intent = new Intent(SignInActivity.this, RegistrationIntentService.class);
startService(intent);

LocalBroadcastManager.getInstance(SignInActivity.this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter("reg complete string"));

}
0
source

Source: https://habr.com/ru/post/1624394/


All Articles