使用异步任务崩溃

时间:2015-04-22 12:49:26

标签: java android asynchronous

为我的问题道歉,但我在android studio中非常棒。

我有一个应用程序,它是一个简单的Web视图应用程序,每次在网站中添加新内容时都会有推送通知。我尝试使用Google云消息进行注册,并且我一直在尝试向三台设备发送推送通知消息,但总是得到相同的结果:

{"multicast_id":5069213226545235739,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}{"registration_ids":["APA91bEl0UpCC7r0BcpYpI-4l_XGSXrUR1cMMQM2it2rIhICX61FQSIBXAN2_fE2INxG8PE2F8gcPtJgHX-Gm78YhPuDO6j1UFU0lUpK....etc"],"data":{"message":"New content available!"}}

我已尝试将服务器密钥更改为浏览器密钥等... 我终于设法擦除共享首选项,重新编译我的应用程序,然后我意识到我的应用程序在后台使用异步任务注册时崩溃了。我之前从未收到过通知,因为reg_id存储在共享的首选项中。我已经从google示例复制了异步任务, 我被困在Android工作室和他的语法和一些帮助将非常方便找到错误是。 这是我的崩溃日志:

FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:200)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
        at java.lang.Thread.run(Thread.java:1019)
 Caused by: java.lang.NullPointerException
        at com.example.alfredo.webapp.MainActivity$1.doInBackground(MainActivity.java:215)
        at com.example.alfredo.webapp.MainActivity$1.doInBackground(MainActivity.java:197)
        at android.os.AsyncTask$2.call(AsyncTask.java:185)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
            at java.util.concurrent.FutureTask.run(FutureTask.java:138)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
            at java.lang.Thread.run(Thread.java:1019)

这是我的代码:

private void registerInBackground() {

        new AsyncTask<Void,Void,String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    regid = gcm.register(SENDER_ID);


                    msg = "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.

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

                    // Save the regid for future use - no need to register again.
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putString(PROPERTY_REG_ID, regid);
                    editor.commit();
                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                }
                return msg;
            }
            // Once registration is done, display the registration status
            // string in the Activity's UI.
            @Override
            protected void onPostExecute(String msg) {
                mDisplay.append(msg + "\n");
            }
        }.execute();
    }

3 个答案:

答案 0 :(得分:0)

初始化gcm = GoogleCloudMessaging.getInstance(this);

答案 1 :(得分:0)

请检查MainActivity.java

中第215行的内容

狂野猜测:无论是'gcm'还是'prefs'。

请在调用.execute();

之前检查它们是否已初始化

答案 2 :(得分:0)

您的代码的第215行是什么? 从您的代码看,gcmprefsmDisplay中的任何一个都可以为null来显示此错误..