简体   繁体   中英

NullPointerException when trying to getStringArray

Here is the relevent code:

...
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];
}

What could be causing this? 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

it seems that res is null for some reason

From what you have posted I'll wager the you are calling setup() for you initialize res... Change your code around to this:

res = getResources();
setup();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM