2013-06-03 63 views
2

我試圖通過搜索類似的問題來解決這個問題, 但是我很抱歉我的問題,它可能是新手問題。問題是,每當我開始一個長循環,它只是暗戀!長循環粉碎我的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 

*感謝您的時間

+1

「......它只是粉碎!」這根本沒有幫助。請發佈堆棧跟蹤。 –

+0

確定只需一秒鐘 – Hamoudaq

+0

另外,您不應該將相同項目的文本視圖設置爲10,000。爲什麼不把它設置爲正確的值?最重要的是,您在主UI線程上執行此操作。 – dhaag23

回答

3

第一次運行時,null通過onCreate()傳遞。你應該檢查null

if(savedInstanceState != null) 
{ 
    QandA =(int[][]) savedInstanceState.get("ArrayOfExer"); 
} 
else 
{ 
    // QandA might be initialized as empty array here 
} 
+0

這是正確的,因爲它是空的,但爲什麼savedInstanceState爲空它應該包含我的數據從其他活動發送? – Hamoudaq

+0

@EngHamoud其他活動通過意圖發送數據。 –

2

你的logcat表明,你得到一個空指針異常。

由於您未在源代碼中包含行號,因此很難確切地知道在哪裏,但我猜QandA爲空,即savedInstanceState.get("ArrayOfExer");返回null。

您不應該在onCreate中執行任何長時間運行的任務,因爲它可能會導致您的活動在啓動過程中超時。如果需要花費大量時間,請在後臺線程上執行此類操作。

哦,順便說一下,您應該閱讀Java命名約定。 類名通常以大寫字符開頭,成員變量和方法名以小寫字符開頭。

堅持約定使其他人更容易閱讀您的代碼。

此外,我討厭'堆',但你應該從來沒有亂七八糟的循環變量,而在循環中。

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

是一個真正的壞事(TM)要做。