1

我正在尝试在我的项目中使用 Google Cloud Messaging (GCM)、PHP 和 MySQL 实现本教程 Android 推送通知

我对其进行了修改,以便设备在应用程序启动时自行注册。我还让它使用一个对话框来显示消息何时到达,但是有一个问题。当我第一次运行该应用程序时,它会尝试注册,但由于我的对话框代码它崩溃了。如果我再次打开它(已经注册)它工作得很好,当我收到消息时,它们会显示在一个对话框中。这是我的修改代码,如果我将其更改为原始作品:

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, Intent intent) {
            final SpannableString newMessage = new SpannableString(intent.getExtras().getString(EXTRA_MESSAGE));
            // Waking up mobile if it is sleeping
            WakeLocker.acquire(getApplicationContext());

            /**
             * Take appropriate action on this message
             * depending upon your app requirement
             * For now i am just displaying it on the screen
             * */

                        final Dialog dialog = new Dialog(context);
                        dialog.setContentView(R.layout.custom);
                        dialog.setTitle("Info");
                        TextView text = (TextView) dialog.findViewById(R.id.text);
                        text.setAutoLinkMask(RESULT_OK);
                        text.setMovementMethod(LinkMovementMethod.getInstance());
                        Linkify.addLinks(newMessage, Linkify.ALL);
                        text.append(newMessage);    
                        //ImageView image = (ImageView) dialog.findViewById(R.id.image);
                        //image.setImageResource(R.drawable.ic_launcher);

                        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                        // if button is clicked, close the custom dialog
                        dialogButton.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });      
                        dialog.show();

            // Showing received message
                    //lblMessage.setText(cucu);
            //lblMessage.setVisibility(View.VISIBLE);
            //btnClear.setVisibility(View.VISIBLE);
            //lblMessage.append(newMessage + "\n");         
            //Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();

            // Releasing wake lock
            WakeLocker.release();
        }
    };

这是原始代码:

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
            // Waking up mobile if it is sleeping
            WakeLocker.acquire(getApplicationContext());

            /**
             * Take appropriate action on this message
             * depending upon your app requirement
             * For now i am just displaying it on the screen
             * */

            // Showing received message
            lblMessage.append(newMessage + "\n");           
            Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();

            // Releasing wake lock
            WakeLocker.release();
        }
    };

这就是 logcat 所说的:

12-07 16:32:55.637: E/AndroidRuntime(27119): FATAL EXCEPTION: main
12-07 16:32:55.637: E/AndroidRuntime(27119): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.androidhive.pushnotifications.DISPLAY_MESSAGE (has extras) } in com.music.aacplay.AACPlayerActivity$1@4052b068
12-07 16:32:55.637: E/AndroidRuntime(27119):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at android.os.Handler.handleCallback(Handler.java:587)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at android.os.Looper.loop(Looper.java:130)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at android.app.ActivityThread.main(ActivityThread.java:3687)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at java.lang.reflect.Method.invokeNative(Native Method)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at java.lang.reflect.Method.invoke(Method.java:507)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at dalvik.system.NativeStart.main(Native Method)
12-07 16:32:55.637: E/AndroidRuntime(27119): Caused by: java.lang.NullPointerException
12-07 16:32:55.637: E/AndroidRuntime(27119):    at android.text.SpannableString.<init>(SpannableString.java:30)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at com.music.aacplay.AACPlayerActivity$1.onReceive(AACPlayerActivity.java:359)
12-07 16:32:55.637: E/AndroidRuntime(27119):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
12-07 16:32:55.637: E/AndroidRuntime(27119):    ... 9 more

所以问题是:我做错了什么导致我的应用程序在第一次运行并尝试注册时崩溃?

4

0 回答 0