gpt4 book ai didi

android - 我在 Android Studio 中收到广播接收器错误作为空点异常。我想接收 GCM 通知

转载 作者:太空宇宙 更新时间:2023-11-03 11:04:56 25 4
gpt4 key购买 nike

我正在使用 GCM 在我的应用程序上获取通知。为此,我在登录和注册时生成注册 ID。在登录或注册过程后,它会抛出 onRecieve 方法的空点异常错误。

这个错误有时会出现,有时不会出现。

这是错误的日志。

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)

这是在登录时生成 token 的代码:

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;
}

最佳答案

你需要在代码中包含你的localbroadcastmanger

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"));

}

关于android - 我在 Android Studio 中收到广播接收器错误作为空点异常。我想接收 GCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34806430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com