0

我正在开发一个为用户显示一些文本视图的应用程序,我认为我会将文本存储在一个数组中并相应地加载信息以从列表视图中选择项目

每个 StringArray 都有一个名称和 4 个项目。我正在使用 if 语句来知道要使用哪个数组,然后相应地设置文本。

但是我似乎无法正确处理。LogCat 给了我 FATALEXCEPTION 并且当我尝试访问该活动时程序立即关闭。

使用代码

if (s.equals("A_DuGamla")) {
    String[] banan_arr = getResources().getStringArray(R.array.hello);
    songname.setText(banan_arr[0]);
    lyricsby.setText(banan_arr[1]);
    melody.setText(banan_arr[2]);
    lyrics.setText(banan_arr[3]);
} else if (s.equals("A_Kungs")) { 
// And so on..

和字符串数组

<string-array name="hello">
    <item>Visa</item>
    <item>Text</item>
    <item>Melodi</item>
    <item>Text</item>
</string-array>

提前致谢

更新错误日志

05-13 21:05:20.771: E/AndroidRuntime(25903): FATAL EXCEPTION: main
05-13 21:05:20.771: E/AndroidRuntime(25903): java.lang.RuntimeException: Unable toinstantiate activity ComponentInfo{se.noxious.visboken/se.noxious.visboken.R1_TradVisor}: java.lang.NullPointerException
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1743)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.os.Looper.loop(Looper.java:150)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.app.ActivityThread.main(ActivityThread.java:4277)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at java.lang.reflect.Method.invoke(Method.java:507)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at dalvik.system.NativeStart.main(Native Method)
05-13 21:05:20.771: E/AndroidRuntime(25903): Caused by: java.lang.NullPointerException
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:356)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:351)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at se.noxious.visboken.R1_TradVisor.<init>(R1_TradVisor.java:19)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at java.lang.Class.newInstanceImpl(Native Method)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at java.lang.Class.newInstance(Class.java:1409)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.app.Instrumentation.newActivity(Instrumentation.java:1040)
05-13 21:05:20.771: E/AndroidRuntime(25903):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1735)
05-13 21:05:20.771: E/AndroidRuntime(25903):    ... 11 more
4

1 回答 1

0

logcat 中的关键行是

at se.noxious.visboken.R1_TradVisor.<init>(R1_TradVisor.java:19)

检查文件 R1_TradVisor.java 中的第 19 行 - 这就是错误所在。无论您在那条线上处理的任何对象都是null( NullPointerException)。

于 2012-05-13T19:29:47.693 回答