doInBackground中的GCM.send异常

时间:2014-02-02 15:06:15

标签: android android-asynctask google-cloud-messaging

我正在尝试将gcm应用到我的应用中。 我在gcm.send命令的doInBackground上遇到错误,我已经检查过参数对于值都很好。

logCat:

02-02 17:01:52.313: E/AndroidRuntime(13881): FATAL EXCEPTION: AsyncTask #2
02-02 17:01:52.313: E/AndroidRuntime(13881): java.lang.RuntimeException: An error occured while executing doInBackground()
02-02 17:01:52.313: E/AndroidRuntime(13881):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at java.lang.Thread.run(Thread.java:856)
02-02 17:01:52.313: E/AndroidRuntime(13881): Caused by: java.lang.NullPointerException
02-02 17:01:52.313: E/AndroidRuntime(13881):    at com.example.catchme2.GoogleMapFragment$10.doInBackground(GoogleMapFragment.java:602)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at com.example.catchme2.GoogleMapFragment$10.doInBackground(GoogleMapFragment.java:1)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-02 17:01:52.313: E/AndroidRuntime(13881):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-02 17:01:52.313: E/AndroidRuntime(13881):    ... 5 more

和代码:

public void sendAmessage ()
    {
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    Bundle data = new Bundle();
                        data.putString("my_message", "Hello World");
                        data.putString("my_action",
                                "com.google.android.gcm.demo.app.ECHO_NOW");
                        String id = Integer.toString(msgId.incrementAndGet());
                        gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);
                        msg = "Sent message";
                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                }
                return msg;
            }

            @Override
            protected void onPostExecute(String msg) {
                 if (progressDialog.isShowing()) {
                        progressDialog.cancel();
                    }

                    MainActivity m = (MainActivity)getActivity();
                    m.LoadSetFrom();
            }
        }.execute();
    }

我不能进入catch语句,因为它在gcm.send行后分解,而从logcat看起来我似乎正在处理asynctask问题。

1 个答案:

答案 0 :(得分:1)

您确定分配给gcm的值不为空吗?您应该通过调用GoogleCloudMessaging.getInstance(this)方法中的onCreate()来获取GCM实例。如果您需要更详细的答案,请发布GoogleMapFragment

的完整代码