在我的Google云消息应用程序中。它将错误显示为空指针异常:println需要android中的消息e。我试图改变代码:
Log.e("UnRegister Receiver Error", "> " + e.getMessage());
为:
String err = (e.getMessage()==null)?"UnRegister Receiver Error":e.getMessage();
Log.e("UnRegister Receiver Error", err);
但它仍然是同样的错误。请帮助我。
@Override
protected void onDestroy() {
if (mRegisterTask != null) {
mRegisterTask.cancel(true);
}
try {
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(this);
} catch (Exception e) {
//String err = (e.getMessage()==null)?"UnRegister Receiver Error":e.getMessage();
//Log.e("UnRegister Receiver Error", err);
Log.e("UnRegister Receiver Error", "> " + e.getMessage());
}
super.onDestroy();
}
Logcat:
02-28 22:58:35.254: E/AndroidRuntime(2812): FATAL EXCEPTION: IntentService[GCMIntentService-414538748674-1]
02-28 22:58:35.254: E/AndroidRuntime(2812): java.lang.NullPointerException: println needs a message
02-28 22:58:35.254: E/AndroidRuntime(2812): at android.util.Log.println_native(Native Method)
02-28 22:58:35.254: E/AndroidRuntime(2812): at android.util.Log.d(Log.java:138)
02-28 22:58:35.254: E/AndroidRuntime(2812): at com.example.fitness2.GCMIntentService.onRegistered(GCMIntentService.java:29)
02-28 22:58:35.254: E/AndroidRuntime(2812): at com.google.android.gcm.GCMBaseIntentService.handleRegistration(GCMBaseIntentService.java:296)
02-28 22:58:35.254: E/AndroidRuntime(2812): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:197)
02-28 22:58:35.254: E/AndroidRuntime(2812): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
02-28 22:58:35.254: E/AndroidRuntime(2812): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 22:58:35.254: E/AndroidRuntime(2812): at android.os.Looper.loop(Looper.java:137)
02-28 22:58:35.254: E/AndroidRuntime(2812): at android.os.HandlerThread.run(HandlerThread.java:60)
02-28 22:58:35.422: W/EGL_emulation(2812): eglSurfaceAttrib not implemented
02-28 22:58:35.774: V/GCMRegistrar(2812): Unregistering receiver
02-28 22:58:35.774: E/UnRegister Receiver Error(2812): Receiver not registered: com.google.android.gcm.GCMBroadcastReceiver@5342460c
02-28 22:58:35.774: E/UnRegister Receiver Error(2812): > Receiver not registered: com.google.android.gcm.GCMBroadcastReceiver@5342460c
答案 0 :(得分:1)
您无需手动打印。您可以使用printStackTrace
执行此操作,如下所示
e.printStackTrace ();
您所要做的就是用此行替换所有catch
代码。
您尝试取消注册尚未注册的广播接收器的logcat
天。 (阅读最后两行)我认为你忘了在开始时调用寄存器接收器。