带有settext的Android StringArray

时间:2012-05-13 18:50:06

标签: android arrays

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

每个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..

和StringArray

<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

1 个答案:

答案 0 :(得分:0)

logcat中的关键行是

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

检查文件R1_TradVisor.java中的第19行 - 这就是错误所在的位置。您在该行上处理的任何对象都是nullNullPointerException)。