GCM 和 android 异步异常
GCM and android Async Exception
我一直在尝试使用 GCM android,该项目最初运行良好,然后突然出现此错误,我一直无法弄清楚。
主要ActivityClass
package com.example.googlecloudmessaging;
import java.io.IOException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class MainActivity extends Activity implements OnClickListener {
Button btnRegId;
EditText etRegId;
GoogleCloudMessaging gcm;
String regid;
String PROJECT_NUMBER = "xxxxxxx";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRegId = (Button) findViewById(R.id.button_get_id);
etRegId = (EditText) findViewById(R.id.edit_text_id);
btnRegId.setOnClickListener(this);
}
public void getRegId() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if(gcm==null)
gcm = GoogleCloudMessaging
.getInstance(getApplicationContext());
regid = gcm.register("PROJECT_NUMBER");
msg = "Device registered, registration ID=" + regid;
Log.i("GCM", "!!!!! " + regid);
} catch (IOException ex) {
msg = "Error: " + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
etRegId.setText(msg + "\n");
}
}.execute(null, null, null);
}
@Override
public void onClick(View v) {
getRegId();
}
}
错误
07-16 15:47:04.453: E/AndroidRuntime(1745): FATAL EXCEPTION: AsyncTask #1
07-16 15:47:04.453: E/AndroidRuntime(1745): Process: com.example.googlecloudmessaging, PID: 1745
07-16 15:47:04.453: E/AndroidRuntime(1745): java.lang.RuntimeException: An error occured while executing doInBackground()
07-16 15:47:04.453: E/AndroidRuntime(1745): at android.os.AsyncTask.done(AsyncTask.java:304)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-16 15:47:04.453: E/AndroidRuntime(1745): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.lang.Thread.run(Thread.java:818)
07-16 15:47:04.453: E/AndroidRuntime(1745): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
07-16 15:47:04.453: E/AndroidRuntime(1745): at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
07-16 15:47:04.453: E/AndroidRuntime(1745): at com.example.googlecloudmessaging.MainActivity.doInBackground(MainActivity.java:50)
07-16 15:47:04.453: E/AndroidRuntime(1745): at com.example.googlecloudmessaging.MainActivity.doInBackground(MainActivity.java:1)
07-16 15:47:04.453: E/AndroidRuntime(1745): at android.os.AsyncTask.call(AsyncTask.java:292)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-16 15:47:04.453: E/AndroidRuntime(1745): ... 4 more
GoogleCloudMessaging 注册和注销方法已弃用。我想你应该使用 InstanceID.getToken().
我一直在尝试使用 GCM android,该项目最初运行良好,然后突然出现此错误,我一直无法弄清楚。
主要ActivityClass
package com.example.googlecloudmessaging;
import java.io.IOException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class MainActivity extends Activity implements OnClickListener {
Button btnRegId;
EditText etRegId;
GoogleCloudMessaging gcm;
String regid;
String PROJECT_NUMBER = "xxxxxxx";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRegId = (Button) findViewById(R.id.button_get_id);
etRegId = (EditText) findViewById(R.id.edit_text_id);
btnRegId.setOnClickListener(this);
}
public void getRegId() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if(gcm==null)
gcm = GoogleCloudMessaging
.getInstance(getApplicationContext());
regid = gcm.register("PROJECT_NUMBER");
msg = "Device registered, registration ID=" + regid;
Log.i("GCM", "!!!!! " + regid);
} catch (IOException ex) {
msg = "Error: " + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
etRegId.setText(msg + "\n");
}
}.execute(null, null, null);
}
@Override
public void onClick(View v) {
getRegId();
}
}
错误
07-16 15:47:04.453: E/AndroidRuntime(1745): FATAL EXCEPTION: AsyncTask #1
07-16 15:47:04.453: E/AndroidRuntime(1745): Process: com.example.googlecloudmessaging, PID: 1745
07-16 15:47:04.453: E/AndroidRuntime(1745): java.lang.RuntimeException: An error occured while executing doInBackground()
07-16 15:47:04.453: E/AndroidRuntime(1745): at android.os.AsyncTask.done(AsyncTask.java:304)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-16 15:47:04.453: E/AndroidRuntime(1745): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.lang.Thread.run(Thread.java:818)
07-16 15:47:04.453: E/AndroidRuntime(1745): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
07-16 15:47:04.453: E/AndroidRuntime(1745): at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
07-16 15:47:04.453: E/AndroidRuntime(1745): at com.example.googlecloudmessaging.MainActivity.doInBackground(MainActivity.java:50)
07-16 15:47:04.453: E/AndroidRuntime(1745): at com.example.googlecloudmessaging.MainActivity.doInBackground(MainActivity.java:1)
07-16 15:47:04.453: E/AndroidRuntime(1745): at android.os.AsyncTask.call(AsyncTask.java:292)
07-16 15:47:04.453: E/AndroidRuntime(1745): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-16 15:47:04.453: E/AndroidRuntime(1745): ... 4 more
GoogleCloudMessaging 注册和注销方法已弃用。我想你应该使用 InstanceID.getToken().