我有一个包含很多行的 for 循环,其中的一行导致 NullPointerException。所以我去在每一行之后放置一个 log.d 来找出导致错误的确切行。
最初,它看起来像这样
for(int i = 0;i<(int)db.countScores();i++){
Log.d("UserLog","hello");
//creating layout to hold views
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//creating gamemode textview
Log.d("UserLog","step 1");
final TextView gameMode = new TextView(this);
Log.d("UserLog","step 2");
gameMode.setText(db.getScoreData("game_type")[i]);
Log.d("UserLog","step 3");
int density = getResources().getDisplayMetrics().densityDpi;
Log.d("UserLog","step 4");
gameMode.setTextSize((int)25 * density);
LayoutParams gameModelp = new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
gameModelp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
gameModelp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Log.d("UserLog","step 5");
gameMode.setLayoutParams(gameModelp);
Log.d("UserLog","step 6");
gameMode.setId(1100+i);
Log.d("UserLog","step 7");
relativeLayout.addView(gameMode);
Log.d("UserLog","step 8");
final TextView corrects = new TextView(this);
Log.d("UserLog","step 9");
corrects.setText(db.getScoreData("corrects")[i]);
Log.d("UserLog","step 10");
corrects.setTextSize((int)25 * density);
... moar code
}
并且输出:一直显示“第 1 步,第 2 步,第 3 步.....”直到 10 并且它因空指针异常而崩溃
在第 10 步之后,我不知道该行出了什么问题,所以我为接下来的 30 行制作了更多日志。具有讽刺意味的是,它从第 1 步一直输出到第 30 步,再次出现空指针异常。
谁能建议我该怎么做?我可以发布完整的代码(没有 log.ds),但它很长。
编辑 - LOGCAT
08-30 14:58:25.477: D/AndroidRuntime(12339): Shutting down VM
08-30 14:58:25.477: W/dalvikvm(12339): threadid=1: thread exiting with uncaught exception (group=0x4165d700)
08-30 14:58:25.487: E/AndroidRuntime(12339): FATAL EXCEPTION: main
08-30 14:58:25.487: E/AndroidRuntime(12339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vsifc.elderlyapp/com.vsifc.elderlyapp.ReviewScoresActivity}: java.lang.NullPointerException
08-30 14:58:25.487: E/AndroidRuntime(12339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
08-30 14:58:25.487: E/AndroidRuntime(12339): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
08-30 14:58:25.487: E/AndroidRuntime(12339): at android.app.ActivityThread.access$600(ActivityThread.java:153)
08-30 14:58:25.487: E/AndroidRuntime(12339): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
08-30 14:58:25.487: E/AndroidRuntime(12339): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 14:58:25.487: E/AndroidRuntime(12339): at android.os.Looper.loop(Looper.java:137)
08-30 14:58:25.487: E/AndroidRuntime(12339): at android.app.ActivityThread.main(ActivityThread.java:5289)
08-30 14:58:25.487: E/AndroidRuntime(12339): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 14:58:25.487: E/AndroidRuntime(12339): at java.lang.reflect.Method.invoke(Method.java:525)
08-30 14:58:25.487: E/AndroidRuntime(12339): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
08-30 14:58:25.487: E/AndroidRuntime(12339): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
08-30 14:58:25.487: E/AndroidRuntime(12339): at dalvik.system.NativeStart.main(Native Method)
08-30 14:58:25.487: E/AndroidRuntime(12339): Caused by: java.lang.NullPointerException
08-30 14:58:25.487: E/AndroidRuntime(12339): at com.vsifc.elderlyapp.ReviewScoresActivity.onCreate(ReviewScoresActivity.java:120)
08-30 14:58:25.487: E/AndroidRuntime(12339): at android.app.Activity.performCreate(Activity.java:5133)
08-30 14:58:25.487: E/AndroidRuntime(12339): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-30 14:58:25.487: E/AndroidRuntime(12339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
08-30 14:58:25.487: E/AndroidRuntime(12339): ... 11 more
完整代码
final TextView gameMode = new TextView(this);
gameMode.setText(db.getScoreData("game_type")[i]);
int density = getResources().getDisplayMetrics().densityDpi;
gameMode.setTextSize((int)25 * density);
LayoutParams gameModelp = new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
gameModelp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
gameModelp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
gameMode.setLayoutParams(gameModelp);
gameMode.setId(1100+i);
relativeLayout.addView(gameMode);
final TextView corrects = new TextView(this);
corrects.setText(db.getScoreData("corrects")[i]);
corrects.setTextSize((int)25 * density);
LayoutParams correctslp = new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
correctslp.addRule(RelativeLayout.BELOW,1100+i);
corrects.setLayoutParams(correctslp);
corrects.setId(1200+i);
relativeLayout.addView(corrects);
final TextView wrongs = new TextView(this);
wrongs.setText(db.getScoreData("wrongs")[i]);
wrongs.setTextSize((int)25 * density);
LayoutParams wrongslp = new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
wrongslp.addRule(RelativeLayout.BELOW,1100+i);
wrongslp.addRule(RelativeLayout.RIGHT_OF,1200+i);
wrongs.setLayoutParams(wrongslp);
wrongs.setId(1300+i);
relativeLayout.addView(wrongs);
final TextView rate = new TextView(this);
int tempValue1 = Integer.parseInt((db.getScoreData("corrects")[i]));
int tempValue2 = Integer.parseInt((db.getScoreData("wrongs")[i]));
float tempValue3 = tempValue1/tempValue2;
tempValue3*=100;
double tempValue4 = Math.floor(tempValue3);
rate.setText(String.valueOf(tempValue4) + "%");
rate.setTextSize((int)25 * density);
LayoutParams ratelp = new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ratelp.addRule(RelativeLayout.BELOW,1100+i);
ratelp.addRule(RelativeLayout.RIGHT_OF,1300+i);
rate.setLayoutParams(wrongslp);
rate.setId(1400+i);
relativeLayout.addView(rate);
theLayout.addView(relativeLayout); //line 120