gpt4 book ai didi

java - 尝试 getStringArray 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 07:43:20 24 4
gpt4 key购买 nike

相关代码如下:

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

这可能是什么原因造成的?日志猫:

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

根据您发布的内容,我打赌您正在调用 setup() 来初始化资源...将您的代码更改为:

res = getResources();
setup();

关于java - 尝试 getStringArray 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11284264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com