按下按钮时,我尝试使用不同参数打开与当前相同的类。我这样做:
Intent j = new Intent("com.test.com.ANSWER");
id = calculateId(0);
j.putExtra("id", Long.toString(id));
startActivityForResult(j, 0);
但我得到一个nullpointer异常..是否有一些规则,你不能在自己内部打开相同的活动?
错误:
12-01 01:29:10.314: E/AndroidRuntime(2194): FATAL EXCEPTION: main
12-01 01:29:10.314: E/AndroidRuntime(2194): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.Answer}: java.lang.NullPointerException
12-01 01:29:10.314: E/AndroidRuntime(2194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-01 01:29:10.314: E/AndroidRuntime(2194): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-01 01:29:10.314: E/AndroidRuntime(2194): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-01 01:29:10.314: E/AndroidRuntime(2194): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-01 01:29:10.314: E/AndroidRuntime(2194): at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 01:29:10.314: E/AndroidRuntime(2194): at android.os.Looper.loop(Looper.java:130)
12-01 01:29:10.314: E/AndroidRuntime(2194): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-01 01:29:10.314: E/AndroidRuntime(2194): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 01:29:10.314: E/AndroidRuntime(2194): at java.lang.reflect.Method.invoke(Method.java:507)
12-01 01:29:10.314: E/AndroidRuntime(2194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
12-01 01:29:10.314: E/AndroidRuntime(2194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
12-01 01:29:10.314: E/AndroidRuntime(2194): at dalvik.system.NativeStart.main(Native Method)
12-01 01:29:10.314: E/AndroidRuntime(2194): Caused by: java.lang.NullPointerException
12-01 01:29:10.314: E/AndroidRuntime(2194): at com.test.Answer.onCreate(Answer.java:86)
12-01 01:29:10.314: E/AndroidRuntime(2194): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-01 01:29:10.314: E/AndroidRuntime(2194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
答案 0 :(得分:1)
我认为您的ID未初始化。您正尝试使用使用id的函数对其进行初始化,因此它永远不会有值。如果不是这样,那么你在不同的地方使用相同的变量名称,这通常不是很好的做法