:尝试调用接口方法' int java.util.List.size()'

时间:2018-01-31 17:23:19

标签: java android

我不知道该怎么做,请帮忙

  

' E / AndroidRuntime:致命异常:主要                     处理:com.is2all.so2al_ijaba,PID:13051                     java.lang.RuntimeException:无法恢复活动{com.is2all.so2al_ijaba / com.is2all.so2al_ijaba.windows_asila}:java.lang.NullPointerException:尝试调用接口方法' int java.util.List.size( )'在null对象引用上                         在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)                         在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)                         在android.app.ActivityThread.-wrap11(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:148)                         在android.app.ActivityThread.main(ActivityThread.java:5417)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)                      引起:java.lang.NullPointerException:尝试调用接口方法' int java.util.List.size()'在null对象引用上                         在com.is2all.so2al_ijaba.windows_asila.table(windows_asila.java:204)                         在com.is2all.so2al_ijaba.windows_asila.onResume(windows_asila.java:439)                         在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)                         在android.app.Activity.performResume(Activity.java:6312)                         在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)'

代码:

public void table() {

        handler.removeCallbacks(run);
        count=30 ;
        rnd = rand.nextInt(mDataList.size());
        id = mDataList.get(rnd).ID ;
        btn.setText(mDataList.get(rnd).Question);
        btn2.setText(mDataList.get(rnd).Answer_1);
        btn3.setText(mDataList.get(rnd).Answer_2);
        btn4.setText(mDataList.get(rnd).Answer_3);
        btn5.setText(mDataList.get(rnd).Answer_4);
        count_b = mDataList.get(rnd).ID_answer;
        timer();
        if (mInterstitialAd.isLoaded() & rnd == 2 || rnd == 4 || rnd == 8 || rnd ==16 || rnd == 32 || rnd == 64 || rnd == 128 ) {
            handler.removeCallbacks(run);
            mInterstitialAd.show();
        }
}

    @Override
    protected void onResume() {
        super.onResume();
        table();

        SharedPreferences savechange =  this.getSharedPreferences
            ("savechange", Context.MODE_PRIVATE);
        int Point = savechange.getInt("Point",point);
        this.point = Point;
        btn7.setText("+نقط : "+point);
    }

2 个答案:

答案 0 :(得分:0)

看起来你的“mDataList”为空,所以当你打电话时 rnd = rand.nextInt(mDataList.size());

它给你错误。

答案 1 :(得分:0)

嗯,问题在您的日志中说明。当您的活动从后台返回并恢复(看起来如此)并且您尝试在空对象引用上调用mDataList.size()时,mDataList为null

您可以随时添加检查以防止您的应用崩溃

if(mDataList != null) {
   rnd = rand.nextInt(mDataList.size());
} 

也许您在onCreate中初始化mDataList,当您的应用程序从后台返回时,它会直接转到onResume,或者您可能根本没有初始化它