GCM android错误接收广播Intent {act = DISPLAY.MESSAGE flg = 0x10(有额外内容)

时间:2014-01-10 19:34:21

标签: java android

启动应用程序并在Gcm中注册设备时遇到麻烦

知道怎么解决吗?

其余的工作正常,我收到通知并且正确。

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            Log.v("MESSAGE", intent.getExtras().getString("message") );
            String newMessage = intent.getExtras().getString("message");
            // Waking up mobile if it is sleeping
            aController.acquireWakeLock(getApplicationContext());       
            Toast.makeText(getApplicationContext(), "Got Message: " + newMessage, Toast.LENGTH_LONG).show();
            // Releasing wake lock
            aController.releaseWakeLock();
        }
    };

错误

01-10 20:11:09.945: W/dalvikvm(20161): threadid=1: thread exiting with uncaught exception (group=0x40c5a1f8)
01-10 20:11:09.945: E/AndroidRuntime(20161): FATAL EXCEPTION: main
01-10 20:11:09.945: E/AndroidRuntime(20161): java.lang.RuntimeException: Error receiving broadcast Intent { act=example.gcm.testing.DISPLAY_MESSAGE flg=0x10 (has extras) } in example.gcm.testing.MainActivity$1@41a0a2d8
01-10 20:11:09.945: E/AndroidRuntime(20161):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
01-10 20:11:09.945: E/AndroidRuntime(20161):    at android.os.Handler.handleCallback(Handler.java:605)
01-10 20:11:09.945: E/AndroidRuntime(20161):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 20:11:09.945: E/AndroidRuntime(20161):    at android.os.Looper.loop(Looper.java:137)
01-10 20:11:09.945: E/AndroidRuntime(20161):    at android.app.ActivityThread.main(ActivityThread.java:4517)
01-10 20:11:09.945: E/AndroidRuntime(20161):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 20:11:09.945: E/AndroidRuntime(20161):    at java.lang.reflect.Method.invoke(Method.java:511)
01-10 20:11:09.945: E/AndroidRuntime(20161):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
01-10 20:11:09.945: E/AndroidRuntime(20161):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
01-10 20:11:09.945: E/AndroidRuntime(20161):    at dalvik.system.NativeStart.main(Native Method)
01-10 20:11:09.945: E/AndroidRuntime(20161): Caused by: java.lang.NullPointerException
01-10 20:11:09.945: E/AndroidRuntime(20161):    at example.gcm.testing.MainActivity$1.onReceive(MainActivity.java:146)
01-10 20:11:09.945: E/AndroidRuntime(20161):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)

2 个答案:

答案 0 :(得分:1)

您的额外代码返回null,

String newMessage = intent.getExtras().getString("message");

使用try catch,并确保从那里收到额外的东西!

答案 1 :(得分:0)

尝试

Log.v("MESSAGE", intent.getExtras().getString("price"));
String newMessage = intent.getExtras().getString("price");