Android - log.d导致程序崩溃

时间:2013-08-30 06:55:58

标签: java android logging nullpointerexception

我有一个包含大量行的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再次出现nullpointerexception。

任何人都可以建议我做什么?我可以发布完整的代码(没有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

http://pastebin.com/bTZcgZMA

完整代码

        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

1 个答案:

答案 0 :(得分:0)

-resolved- 复制粘贴: 我的坏,第120行就在它之后。我解决了这个问题,显然这是因为错误地定义了布局。对不起造成的所有麻烦。但是现在我有一个关于视图不可见的单独问题,但我想我会为那个

创建一个单独的问题