我試圖通過搜索類似的問題來解決這個問題, 但是我很抱歉我的問題,它可能是新手問題。問題是,每當我開始一個長循環,它只是暗戀!長循環粉碎我的Android應用程序
例如,如果我有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--;
}
}
}
}
logcat的
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= [email protected]9e4a30
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
*感謝您的時間
「......它只是粉碎!」這根本沒有幫助。請發佈堆棧跟蹤。 –
確定只需一秒鐘 – Hamoudaq
另外,您不應該將相同項目的文本視圖設置爲10,000。爲什麼不把它設置爲正確的值?最重要的是,您在主UI線程上執行此操作。 – dhaag23