我无法弄清楚为什么这种情况会持续发生。花了很多时间试图弄清楚这一点,尽管已经问了很多类似的问题。
仅发布相关代码。
Convo1.java
public class Convo1 extends Activity {
Convo1 convo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_convo1);
convo=this;
SetQn.setquestion(convo);
}
SetQn.java
public class SetQn extends Application {
public static void setquestion(Activity activity){
String getqn = GetConvo.getqn(activity);
}
GetConvo.java
public class GetConvo extends Activity {
public static String getqn(Activity activity) {
int newqn=activity.getResources().getIdentifier("a", "string","com.kings.englishforretail");
String opt1 = activity.getString(newqn);
return opt1;
}
Logcat
11-24 06:32:26.559: D/dalvikvm(6992): GC_FOR_ALLOC freed 83K, 5% free 2842K/2980K, paused 2ms, total 4ms
11-24 06:32:26.559: I/dalvikvm-heap(6992): Grow heap (frag case) to 3.733MB for 950412-byte allocation
11-24 06:32:26.579: D/dalvikvm(6992): GC_FOR_ALLOC freed 7K, 4% free 3763K/3912K, paused 18ms, total 18ms
11-24 06:32:26.609: D/dalvikvm(6992): GC_FOR_ALLOC freed 2K, 4% free 3777K/3912K, paused 2ms, total 2ms
11-24 06:32:26.609: I/dalvikvm-heap(6992): Grow heap (frag case) to 4.581MB for 880892-byte allocation
11-24 06:32:26.629: D/dalvikvm(6992): GC_FOR_ALLOC freed <1K, 3% free 4637K/4776K, paused 18ms, total 18ms
11-24 06:32:26.629: W/string(6992): 2131361805
11-24 06:32:26.639: D/AndroidRuntime(6992): Shutting down VM
11-24 06:32:26.639: W/dalvikvm(6992): threadid=1: thread exiting with uncaught exception (group=0xb3d17b20)
11-24 06:32:26.639: E/AndroidRuntime(6992): FATAL EXCEPTION: main
11-24 06:32:26.639: E/AndroidRuntime(6992): Process: com.kings.englishforretail, PID: 6992
11-24 06:32:26.639: E/AndroidRuntime(6992): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kings.englishforretail/com.kings.englishforretail.Convo1}: java.lang.NullPointerException
11-24 06:32:26.639: E/AndroidRuntime(6992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-24 06:32:26.639: E/AndroidRuntime(6992): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-24 06:32:26.639: E/AndroidRuntime(6992): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-24 06:32:26.639: E/AndroidRuntime(6992): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-24 06:32:26.639: E/AndroidRuntime(6992): at android.os.Handler.dispatchMessage(Handler.java:102)
11-24 06:32:26.639: E/AndroidRuntime(6992): at android.os.Looper.loop(Looper.java:136)
11-24 06:32:26.639: E/AndroidRuntime(6992): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-24 06:32:26.639: E/AndroidRuntime(6992): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 06:32:26.639: E/AndroidRuntime(6992): at java.lang.reflect.Method.invoke(Method.java:515)
11-24 06:32:26.639: E/AndroidRuntime(6992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-24 06:32:26.639: E/AndroidRuntime(6992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-24 06:32:26.639: E/AndroidRuntime(6992): at dalvik.system.NativeStart.main(Native Method)
11-24 06:32:26.639: E/AndroidRuntime(6992): Caused by: java.lang.NullPointerException
11-24 06:32:26.639: E/AndroidRuntime(6992): at com.kings.englishforretail.SetQn.setquestion(SetQn.java:21)
11-24 06:32:26.639: E/AndroidRuntime(6992): at com.kings.englishforretail.Convo1.onCreate(Convo1.java:34)
11-24 06:32:26.639: E/AndroidRuntime(6992): at android.app.Activity.performCreate(Activity.java:5231)
11-24 06:32:26.639: E/AndroidRuntime(6992): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-24 06:32:26.639: E/AndroidRuntime(6992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-24 06:32:26.639: E/AndroidRuntime(6992): ... 11 more
编辑:
获得正确的输出
int newqn=activity.getResources().getIdentifier("a","string","com.kings.englishforretail");
只是
activity.getString(newqn);
这给了我错误。
最佳答案
SetQn.setquestion(convo); //这里convo可能返回错误的上下文
您可以尝试以下
SetQn.setquestion(this);