例外Android Google Cloud消息寄存器

Flo*_*034 4 android google-cloud-messaging

当我尝试将我的应用程序设备注册到服务器时,我收到此错误:我已经像应用程序一样导入了gogole-play-services_lib,并在我的应用程序中添加了这个库,在Properties-> Android-> Add中

    12-10 16:00:03.026: E/AndroidRuntime(29362): FATAL EXCEPTION: AsyncTask #1
12-10 16:00:03.026: E/AndroidRuntime(29362): Process: com.appehour.jdm, PID: 29362
12-10 16:00:03.026: E/AndroidRuntime(29362): java.lang.RuntimeException: An error occured while executing doInBackground()
12-10 16:00:03.026: E/AndroidRuntime(29362):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.lang.Thread.run(Thread.java:841)
12-10 16:00:03.026: E/AndroidRuntime(29362): Caused by: java.lang.NullPointerException
12-10 16:00:03.026: E/AndroidRuntime(29362):    at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:478)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at android.app.PendingIntent.getBroadcast(PendingIntent.java:467)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at com.google.android.gms.gcm.GoogleCloudMessaging.d(Unknown Source)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at com.google.android.gms.gcm.GoogleCloudMessaging.b(Unknown Source)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at com.appehour.jdm.TestPush$registerInBackground.doInBackground(TestPush.java:172)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at com.appehour.jdm.TestPush$registerInBackground.doInBackground(TestPush.java:1)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-10 16:00:03.026: E/AndroidRuntime(29362):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-10 16:00:03.026: E/AndroidRuntime(29362):    ... 4 more
Run Code Online (Sandbox Code Playgroud)

这是我在后台的代码:

 @Override
        protected String doInBackground(String... params) {


            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }

                regid = gcm.register(SENDER_ID);
                msg = "Device registered, registration ID=" + regid;
                Log.d(TAG_LOGS, "Device registered, registration ID=" + regid);

                // You should send the registration ID to your server over HTTP,
                // so it can use GCM/HTTP or CCS to send messages to your app.
                // The request to your server should be authenticated if your app
                // is using accounts.
                sendRegistrationIdToBackend();

                // For this demo: we don't need to send it because the device
                // will send upstream messages to a server that echo back the
                // message using the 'from' address in the message.

                // Persist the regID - no need to register again.
                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
                Log.e(TAG_LOGS, "Error :" + ex.getMessage());
                // If there is an error, don't just keep trying to register.
                // Require the user to click a button again, or perform
                // exponential back-off.
            }
            return msg;
        }
     }
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决它.谢谢.

Fab*_*ank 6

这个非常棘手.验证

GoogleCloudMessaging.getInstance(context);
Run Code Online (Sandbox Code Playgroud)

没有使用null上下文运行.对我来说就是这种情况,我得到了你所描述的错误.在我修复了我的上下文(我在我的帮助器类构造函数中忘记了mContext = ctx)后,错误消失了.