以下是相关代码:
...
Resources res;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.variable);
res = getResources();
...
}
protected void setup() {
String text = res.getStringArray(R.array.categories)[4];
}
可能导致这种情况的原因是什么? logcat的:
07-01 13:37:01.988: E/AndroidRuntime(13905): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.centauri.equations/com.centauri.equations.activity.physics.WorkActivity}: java.lang.NullPointerException
07-01 13:37:01.988: E/AndroidRuntime(13905): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-01 13:37:01.988: E/AndroidRuntime(13905): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-01 13:37:01.988: E/AndroidRuntime(13905): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-01 13:37:01.988: E/AndroidRuntime(13905): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-01 13:37:01.988: E/AndroidRuntime(13905): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 13:37:01.988: E/AndroidRuntime(13905): at android.os.Looper.loop(Looper.java:137)
07-01 13:37:01.988: E/AndroidRuntime(13905): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-01 13:37:01.988: E/AndroidRuntime(13905): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 13:37:01.988: E/AndroidRuntime(13905): at java.lang.reflect.Method.invoke(Method.java:511)
07-01 13:37:01.988: E/AndroidRuntime(13905): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-01 13:37:01.988: E/AndroidRuntime(13905): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-01 13:37:01.988: E/AndroidRuntime(13905): at dalvik.system.NativeStart.main(Native Method)
07-01 13:37:01.988: E/AndroidRuntime(13905): Caused by: java.lang.NullPointerException
07-01 13:37:01.988: E/AndroidRuntime(13905): at com.centauri.equations.activity.physics.WorkActivity.setup(WorkActivity.java:119)
07-01 13:37:01.988: E/AndroidRuntime(13905): at com.centauri.equations.activity.Categories.onCreate(Categories.java:95)
07-01 13:37:01.988: E/AndroidRuntime(13905): at com.centauri.equations.activity.physics.WorkActivity.onCreate(WorkActivity.java:34)
07-01 13:37:01.988: E/AndroidRuntime(13905): at android.app.Activity.performCreate(Activity.java:4465)
07-01 13:37:01.988: E/AndroidRuntime(13905): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-01 13:37:01.988: E/AndroidRuntime(13905): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-01 13:37:01.988: E/AndroidRuntime(13905): ... 11 more
答案 0 :(得分:2)
由于某种原因,res似乎为空
根据您发布的内容,我会打赌您正在调用setup()来初始化res ...将代码更改为:
res = getResources();
setup();