我试图通过搜索类似的问题来解决这个问题, 但是我很抱歉我的问题可能是新手问题。问题是每当我开始一个长循环时它只是粉碎!
例如,如果我有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= 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
*谢谢你的时间
答案 0 :(得分:3)
首次运行时,null
将通过onCreate()
传递。您应该检查null
:
if(savedInstanceState != null)
{
QandA =(int[][]) savedInstanceState.get("ArrayOfExer");
}
else
{
// QandA might be initialized as empty array here
}
答案 1 :(得分:2)
您的logcat显示您正在获取NullPointer异常。
由于您的源代码中没有包含行号,因此很难确切地告诉它在哪里,但我猜测QandA
为空,即savedInstanceState.get("ArrayOfExer");
返回null。
您不应该在onCreate中执行任何长时间运行的任务,因为它可能会导致您的Activity在启动期间超时。如果需要花费大量时间,请在后台线程上执行此类操作。
哦,顺便说一句,您应该阅读Java命名约定。 类名通常以大写字符开头,成员变量和方法名称以小写字符开头。
坚持这些惯例使其他人更容易阅读您的代码。
另外,我讨厌'堆积',但你应该永远在循环中混淆循环变量。
} catch (Exception e) {
System.out.println(e.getMessage());
i--;
}
是一个真正的Bad Thing(TM)。