好的,这样我的strings.xml文件中就有5个数组,其中都包含5个元素,如下所示:

<string-array name="monday">
        <item>monday</item>
        <item>tuesday</item>
        <item>wednesday</item>
        <item>thursday</item>
        <item>friday</item>
    </string-array>


现在我的代码中有一个switch语句,该语句取决于他们单击的按钮,这会将这5个数组之一设置为我的主数组。但是我尝试了许多不同的方法,但是我仍然在这一行上得到空指针异常:

Days.masterarray = getResources().getStringArray(R.array.monday);


也做到了这一点,但得到了相同的结果:

Days.setMasterarray(getResources().getStringArray(R.array.monday));


主阵列:

public static String[] masterarray = new String[] { "monday", "tuesday",
            "wednesday", "thursday", "friday" };


我给它默认值开始。现在,当我运行switch语句时,我得到以下信息:

03-19 13:43:23.555: WARN/System.err(1379): java.lang.NullPointerException
03-19 13:43:23.586: WARN/System.err(1379):     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
03-19 13:43:23.586: WARN/System.err(1379):     at schoolBook.Icestone.Days.setArray(Days.java:77)
03-19 13:43:23.595: WARN/System.err(1379):     at schoolBook.Icestone.SampleActivity.onCreate(SampleActivity.java:27)
03-19 13:43:23.605: WARN/System.err(1379):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-19 13:43:23.626: WARN/System.err(1379):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
03-19 13:43:23.626: WARN/System.err(1379):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1577)
03-19 13:43:23.635: WARN/System.err(1379):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:130)
03-19 13:43:23.644: WARN/System.err(1379):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:342)
03-19 13:43:23.655: WARN/System.err(1379):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:680)
03-19 13:43:23.676: WARN/System.err(1379):     at android.widget.TabHost.setCurrentTab(TabHost.java:349)
03-19 13:43:23.676: WARN/System.err(1379):     at android.widget.TabHost.addTab(TabHost.java:239)
03-19 13:43:23.695: WARN/System.err(1379):     at schoolBook.Icestone.Days.onCreate(Days.java:32)
03-19 13:43:23.695: WARN/System.err(1379):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-19 13:43:23.695: WARN/System.err(1379):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
03-19 13:43:23.716: WARN/System.err(1379):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
03-19 13:43:23.716: WARN/System.err(1379):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-19 13:43:23.716: WARN/System.err(1379):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
03-19 13:43:23.735: WARN/System.err(1379):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 13:43:23.735: WARN/System.err(1379):     at android.os.Looper.loop(Looper.java:126)
03-19 13:43:23.744: WARN/System.err(1379):     at android.app.ActivityThread.main(ActivityThread.java:3997)
03-19 13:43:23.754: WARN/System.err(1379):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 13:43:23.754: WARN/System.err(1379):     at java.lang.reflect.Method.invoke(Method.java:491)
03-19 13:43:23.754: WARN/System.err(1379):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-19 13:43:23.775: WARN/System.err(1379):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-19 13:43:23.775: WARN/System.err(1379):     at dalvik.system.NativeStart.main(Native Method)


任何有关如何解决此问题的光将不胜感激

最佳答案

我认为getResources可能为null。尝试之一


在该行上放置一个断点并调试以查看其是否为空(如果不为空,则查找为空)
使用上下文。即。
mContext.getResources().getStringArray(R.array.monday));

10-04 21:29