0

按下按钮时,我尝试使用不同的参数打开与当前所在的类相同的类。我这样做:

Intent j = new Intent("com.test.com.ANSWER");
            id = calculateId(0);
            j.putExtra("id", Long.toString(id));
            startActivityForResult(j, 0);

但是我得到了一个空指针异常。是否有一些规则不能在其内部打开相同的活动?

错误:

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)
4

1 回答 1

1

我认为你的 id 没有被初始化。您正在尝试使用使用 id 的函数对其进行初始化,因此它永远不会有值。如果不是这样,那么您在不同的地方使用相同的变量名,这通常不是很好的做法

于 2012-12-01T01:08:31.493 回答