我正在尝试初始化多个文本视图,但它给了我一个NULL指针异常
这是我的代码:
TableRow headerTableRow = new TableRow(this);
TextView headerTextView[][] = new TextView[2][9];
for(TextView tmp[]:headerTextView){
for(TextView tmp1:tmp){
tmp1 = new TextView(this);
tmp1.setText("");
}
}
headerTextView[0][0].setText("Igralka");
headerTextView[0][1].setText("Goli");
headerTextView[0][2].setText("Streli");
headerTextView[0][3].setText("Procenti");
headerTextView[0][4].setText("Pridobljene");
headerTextView[0][5].setText("Izgubljene");
headerTextView[0][6].setText("Asistence");
headerTextView[0][7].setText("Bloki");
headerTextView[0][8].setText("Prekrški");
for(TextView tmp:headerTextView[0]){
headerTableRow.addView(tmp);
}
有logcat:
04-20 17:14:59.275: E/AndroidRuntime(20420): FATAL EXCEPTION: main
04-20 17:14:59.275: E/AndroidRuntime(20420): Process: com.kkogovsek.rokomentastatistika, PID: 20420
04-20 17:14:59.275: E/AndroidRuntime(20420): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kkogovsek.rokomentastatistika/com.kkogovsek.rokomentastatistika.TekmaLive}: java.lang.NullPointerException
04-20 17:14:59.275: E/AndroidRuntime(20420): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
04-20 17:14:59.275: E/AndroidRuntime(20420): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
04-20 17:14:59.275: E/AndroidRuntime(20420): at android.app.ActivityThread.access$800(ActivityThread.java:145)
04-20 17:14:59.275: E/AndroidRuntime(20420): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
04-20 17:14:59.275: E/AndroidRuntime(20420): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 17:14:59.275: E/AndroidRuntime(20420): at android.os.Looper.loop(Looper.java:136)
04-20 17:14:59.275: E/AndroidRuntime(20420): at android.app.ActivityThread.main(ActivityThread.java:5088)
04-20 17:14:59.275: E/AndroidRuntime(20420): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 17:14:59.275: E/AndroidRuntime(20420): at java.lang.reflect.Method.invoke(Method.java:515)
04-20 17:14:59.275: E/AndroidRuntime(20420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-20 17:14:59.275: E/AndroidRuntime(20420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-20 17:14:59.275: E/AndroidRuntime(20420): at dalvik.system.NativeStart.main(Native Method)
04-20 17:14:59.275: E/AndroidRuntime(20420): Caused by: java.lang.NullPointerException
04-20 17:14:59.275: E/AndroidRuntime(20420): at com.kkogovsek.rokomentastatistika.TekmaLive.generateContent(TekmaLive.java:48)
04-20 17:14:59.275: E/AndroidRuntime(20420): at com.kkogovsek.rokomentastatistika.TekmaLive.onCreate(TekmaLive.java:30)
04-20 17:14:59.275: E/AndroidRuntime(20420): at android.app.Activity.performCreate(Activity.java:5434)
04-20 17:14:59.275: E/AndroidRuntime(20420): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-20 17:14:59.275: E/AndroidRuntime(20420): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
我只是暂时使用一个维度,所以它没有错!!!!
答案 0 :(得分:1)
问题是for each循环不能用于替换或创建数组中的元素,因为它使用临时变量。因此所做的更改不会反映在原始数组中。
使用普通for循环:
TextView headerTextView[][] = new TextView[2][9];
for(int i = 0; i < headerTextView.length; i++){
for(int j = 0; j < headerTextView[i].length; j++){
headerTextView[i][j] = new TextView(this);
headerTextView[i][j].setText("");
}
}