gpt4 book ai didi

android - GCM和android异步异常

转载 作者:行者123 更新时间:2023-12-02 03:23:48 26 4
gpt4 key购买 nike

我一直在尝试与GCM for android一起使用,该项目最初运行良好,然后突然我收到了此错误,但我一直无法弄清。

主要 Activity 类别

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$3.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$1.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$1.doInBackground(MainActivity.java:50)
07-16 15:47:04.453: E/AndroidRuntime(1745): at com.example.googlecloudmessaging.MainActivity$1.doInBackground(MainActivity.java:1)
07-16 15:47:04.453: E/AndroidRuntime(1745): at android.os.AsyncTask$2.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()。

关于android - GCM和android异步异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31452895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com