我的 android 应用程序出现问题。
我使用字符串数组在 TextView 中显示项目,但每次我得到 NullpointerException,你能帮我看看我错在哪里吗?
这是代码:
public class test extends Activity {
String [] myString ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
myString = getResources().getStringArray(R.array.myArray);
String q = myString[1];
TextView tv = (TextView) findViewById(R.id.text2);
tv.setText(q);
}
}
和我的 xml 代码:
<string-array name="myArray">
<item > string 1</item>
<item > string 2</item>
<item> string 3</item>
<item > string 4</item>
</string-array>
日志猫:
04-15 11:38:27.647: E/AndroidRuntime(285): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.test}: java.lang.NullPointerException
04-15 11:38:27.647: E/AndroidRuntime(285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-15 11:38:27.647: E/AndroidRuntime(285): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-15 11:38:27.647: E/AndroidRuntime(285): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-15 11:38:27.647: E/AndroidRuntime(285): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-15 11:38:27.647: E/AndroidRuntime(285): at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 11:38:27.647: E/AndroidRuntime(285): at android.os.Looper.loop(Looper.java:123)
04-15 11:38:27.647: E/AndroidRuntime(285): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-15 11:38:27.647: E/AndroidRuntime(285): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 11:38:27.647: E/AndroidRuntime(285): at java.lang.reflect.Method.invoke(Method.java:521)
04-15 11:38:27.647: E/AndroidRuntime(285): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-15 11:38:27.647: E/AndroidRuntime(285): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-15 11:38:27.647: E/AndroidRuntime(285): at dalvik.system.NativeStart.main(Native Method)
04-15 11:38:27.647: E/AndroidRuntime(285): Caused by: java.lang.NullPointerException
04-15 11:38:27.647: E/AndroidRuntime(285): at com.example.app.test.onCreate(vovochka.java:29)
04-15 11:38:27.647: E/AndroidRuntime(285): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-15 11:38:27.647: E/AndroidRuntime(285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)