2

我试图通过搜索类似的问题来解决这个问题,但是我很抱歉我的问题可能是新手问题。问题是每当我开始一个长循环时,它就会被粉碎!

例如,如果我TextView想要设置文本为 0 并执行循环以将值增加到 10,例如它将崩溃!即使使用线程它仍然会崩溃。

这是我的代码:

package com.example.mathexercises;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.widget.ProgressBar;
import android.widget.TextView;

public class level1 extends Activity {
    TextView QuestionLable;
    TextView TimeLable;
    int QandA[][];
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.level1);
        QuestionLable = (TextView) findViewById(R.id.Level1QuestionLable);
        TimeLable     = (TextView) findViewById(R.id.Level1Time);
        QandA =(int[][]) savedInstanceState.get("ArrayOfExer");

        int sum=0;
        String Qformated="";
        TimeLable.setText("Started !!");
        for (int i = 0; QandA !=null && i<QandA.length; i++) {
            try {
                Qformated = "" + QandA[i][0];
                switch (QandA[i][1]) {
                case -1:
                    Qformated += " +";
                    break;
                case -2:
                    Qformated += " -";
                    break;
                case -3:
                    Qformated += " X";
                    break;
                case -4:
                    Qformated += " /";
                    break;

                }

                Qformated += " " + QandA[i][2];
                sum+=QandA[i][3];
                QuestionLable.setText(Qformated);

            } catch (Exception e) {
                System.out.println(e.getMessage());
                i--;
            }
        }
    }
}

日志猫

06-04 01:24:42.875: D/dalvikvm(24925): Late-enabling CheckJNI
06-04 01:24:43.275: D/libEGL(24925): loaded /system/lib/egl/libEGL_mali.so
06-04 01:24:43.300: D/libEGL(24925): loaded /system/lib/egl/libGLESv1_CM_mali.so
06-04 01:24:43.305: D/libEGL(24925): loaded /system/lib/egl/libGLESv2_mali.so
06-04 01:24:43.365: D/(24925): Device driver API match
06-04 01:24:43.365: D/(24925): Device driver API version: 10
06-04 01:24:43.365: D/(24925): User space API version: 10 
06-04 01:24:43.365: D/(24925): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
06-04 01:24:43.425: D/OpenGLRenderer(24925): Enabling debug mode 0
06-04 01:24:43.485: E/SensorManager(24925): thread start
06-04 01:24:43.500: D/SensorManager(24925): registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@429e4a30
06-04 01:24:50.705: D/AndroidRuntime(24925): Shutting down VM
06-04 01:24:50.705: W/dalvikvm(24925): threadid=1: thread exiting with uncaught exception (group=0x41d692a0)
06-04 01:24:50.715: E/AndroidRuntime(24925): FATAL EXCEPTION: main
06-04 01:24:50.715: E/AndroidRuntime(24925): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathexercises/com.example.mathexercises.level1}: java.lang.NullPointerException
06-04 01:24:50.715: E/AndroidRuntime(24925):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at android.os.Looper.loop(Looper.java:137)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at android.app.ActivityThread.main(ActivityThread.java:4898)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at java.lang.reflect.Method.invokeNative(Native Method)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at java.lang.reflect.Method.invoke(Method.java:511)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at dalvik.system.NativeStart.main(Native Method)
06-04 01:24:50.715: E/AndroidRuntime(24925): Caused by: java.lang.NullPointerException
06-04 01:24:50.715: E/AndroidRuntime(24925):    at com.example.mathexercises.level1.onCreate(level1.java:26)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at android.app.Activity.performCreate(Activity.java:5206)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
06-04 01:24:50.715: E/AndroidRuntime(24925):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
06-04 01:24:50.715: E/AndroidRuntime(24925):    ... 11 more

*感谢您的时间

4

2 回答 2

3

在第一次运行时,null通过onCreate(). 你应该检查null

if(savedInstanceState != null)
{
    QandA =(int[][]) savedInstanceState.get("ArrayOfExer");
}
else
{
    // QandA might be initialized as empty array here
}
于 2013-06-03T22:42:39.040 回答
2

您的 logcat 显示您收到 NullPointer 异常。

由于您没有在源代码中包含行号,因此很难确切地知道在哪里,但我猜那QandA是空的,即savedInstanceState.get("ArrayOfExer");返回空。

您不应该在 onCreate 中执行任何长时间运行的任务,因为它可能会导致您的 Activity 在启动期间超时。如果需要花费大量时间,请在后台线程上执行此类操作。

哦,顺便说一下,您可能应该阅读 Java 命名约定。类名通常以大写字符开头,成员变量和方法名以小写字符开头。

遵守约定可以让其他人更容易阅读您的代码。

另外,我讨厌“堆积”,但你不应该循环中弄乱循环变量。

        } catch (Exception e) {
            System.out.println(e.getMessage());
            i--;
        }

是一件真正的坏事(TM)。

于 2013-06-03T22:39:47.417 回答