我有一个已经在GCM注册的设备。我试过搜索,但我找不到任何类似的问题。大多数用户似乎在让getRegistrationId返回非空字符串时遇到问题。关于为什么会导致错误的任何想法?
public class UploadOption extends Activity{
Intent extras = getIntent();
String optionid = extras.getStringExtra("KEY_OPTION");
String duration = extras.getStringExtra("KEY_DURATION");
String reward = extras.getStringExtra("KEY_REWARD");
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.debug_layout);
final String regId = GCMRegistrar.getRegistrationId(this);
TextView tvDebug = (TextView) findViewById(R.id.tvDebug);
tvDebug.setText(regId);
// Stores option id, php grabs option.
//new Upload().execute();
}
}
错误日志如下:
01-18 18:02:51.756: D/AndroidRuntime(4478): Shutting down VM
01-18 18:02:51.756: W/dalvikvm(4478): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
01-18 18:02:51.803: E/AndroidRuntime(4478): FATAL EXCEPTION: main
01-18 18:02:51.803: E/AndroidRuntime(4478): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.matt.drapp/com.matt.drapp.UploadOption}: java.lang.NullPointerException
01-18 18:02:51.803: E/AndroidRuntime(4478): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
01-18 18:02:51.803: E/AndroidRuntime(4478): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-18 18:02:51.803: E/AndroidRuntime(4478): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-18 18:02:51.803: E/AndroidRuntime(4478): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-18 18:02:51.803: E/AndroidRuntime(4478): at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 18:02:51.803: E/AndroidRuntime(4478): at android.os.Looper.loop(Looper.java:130)
01-18 18:02:51.803: E/AndroidRuntime(4478): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-18 18:02:51.803: E/AndroidRuntime(4478): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 18:02:51.803: E/AndroidRuntime(4478): at java.lang.reflect.Method.invoke(Method.java:507)
01-18 18:02:51.803: E/AndroidRuntime(4478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-18 18:02:51.803: E/AndroidRuntime(4478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-18 18:02:51.803: E/AndroidRuntime(4478): at dalvik.system.NativeStart.main(Native Method)
01-18 18:02:51.803: E/AndroidRuntime(4478): Caused by: java.lang.NullPointerException
01-18 18:02:51.803: E/AndroidRuntime(4478): at com.matt.drapp.UploadOption.<init>(UploadOption.java:32)
01-18 18:02:51.803: E/AndroidRuntime(4478): at java.lang.Class.newInstanceImpl(Native Method)
01-18 18:02:51.803: E/AndroidRuntime(4478): at java.lang.Class.newInstance(Class.java:1409)
01-18 18:02:51.803: E/AndroidRuntime(4478): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-18 18:02:51.803: E/AndroidRuntime(4478): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
01-18 18:02:51.803: E/AndroidRuntime(4478): ... 11 more
答案 0 :(得分:3)
问题是由Intent
代码引起的。请记住,活动仅在onCreate()
之后初始化,并且您在活动初始化之前尝试获取Intent值。
将以下代码放在onCreate();
Intent extras = getIntent();
String optionid = extras.getStringExtra("KEY_OPTION");
String duration = extras.getStringExtra("KEY_DURATION");
String reward = extras.getStringExtra("KEY_REWARD");
它应该有用。
答案 1 :(得分:1)
示例中的哪一行导致NullPointerException。因为如果它只是找不到textView那么可能导致整个应用程序崩溃只是因为你尝试设置文本而它是一个空对象。
首先,请注释textview的内容并查看它是否仍然有效。如果不是这样,你能不能告诉我们你班上哪一行是第32行,因为它显然与此处不一样。
答案 2 :(得分:0)
问题是你的getRegistratonID返回null。这是因为您在实际注册GCM服务器之前在活动类中调用它,并接收到GCMIntentService的onRegistered回调。如果getRegistrationID返回null,则应触发AsyncTask以调用GCMRegistrar.register。
按照说明here: