2013-01-20 41 views
0

我想在我的Adroid應用程序中使用進度對話框與異步任務。但是當我運行我的應用程序時,這導致了致命異常。當我用另一種方法使用它時,它正常工作。但是當我用下面的方法使用它時,它會導致錯誤。我讀過其他問題,這是因爲Array,但我不知道它是否屬實。致命的異常Syntask#1

這裏是我的活動代碼:

private class TheTask extends AsyncTask<Void, Void, Void>{ 
    @Override 
    protected void onPreExecute() { 
    authProgressDialog = ProgressDialog.show(Front9Activity.this, "", "Authenticating...", true, false); 
    } 
    @Override 
    protected Void doInBackground(Void... params) { 
    loading(); 
    return null; 
    } 
    @Override 
    protected void onPostExecute(Void result) { 
    if(authProgressDialog.isShowing()) { 
     authProgressDialog.dismiss(); 
    } 
    return; 
    } 
} 
public void loading(){ 
    mode = Integer.parseInt(gametype); 
    check = Integer.parseInt(type); 
    if(mode == 1) { 
    name = name1; 
    number = mobile1; 
    } else if(mode == 2) { 
    name = name2; 
    number = mobile2; 
    } else if(mode == 3) { 
    name = name3; 
    number = mobile3; 
    } else { 
    name = name4; 
    number = mobile4; 
    } 
    if(check == 1){ 
    data = "Solo"; 
    } else if(check == 2) { 
    data = "GroupScore"; 
    } else { 
    data = "Tournament"; 
    } 
    save = openOrCreateDatabase("clubscores", MODE_PRIVATE,null); 
    Cursor c = save.rawQuery("Select *from Courses where VenueName = '"+ courses +"'", null); 
    c.moveToFirst(); 
    ((TextView) findViewById(R.id.front9hole1)).setText(c.getString(c.getColumnIndex("Hole1"))); 
    ((TextView) findViewById(R.id.front9hole2)).setText(c.getString(c.getColumnIndex("Hole2"))); 
    ((TextView) findViewById(R.id.front9hole3)).setText(c.getString(c.getColumnIndex("Hole3"))); 
    ((TextView) findViewById(R.id.front9hole4)).setText(c.getString(c.getColumnIndex("Hole4"))); 
    ((TextView) findViewById(R.id.front9hole5)).setText(c.getString(c.getColumnIndex("Hole5"))); 
    ((TextView) findViewById(R.id.front9hole6)).setText(c.getString(c.getColumnIndex("Hole6"))); 
    ((TextView) findViewById(R.id.front9hole7)).setText(c.getString(c.getColumnIndex("Hole7"))); 
    ((TextView) findViewById(R.id.front9hole8)).setText(c.getString(c.getColumnIndex("Hole8"))); 
    ((TextView) findViewById(R.id.front9hole9)).setText(c.getString(c.getColumnIndex("Hole9"))); 
    ((TextView) findViewById(R.id.front9hole10)).setText(c.getString(c.getColumnIndex("Hole10"))); 
    ((TextView) findViewById(R.id.front9hole11)).setText(c.getString(c.getColumnIndex("Hole11"))); 
    ((TextView) findViewById(R.id.front9hole12)).setText(c.getString(c.getColumnIndex("Hole12"))); 
    ((TextView) findViewById(R.id.front9hole13)).setText(c.getString(c.getColumnIndex("Hole13"))); 
    ((TextView) findViewById(R.id.front9hole14)).setText(c.getString(c.getColumnIndex("Hole14"))); 
    ((TextView) findViewById(R.id.front9hole15)).setText(c.getString(c.getColumnIndex("Hole15"))); 
    ((TextView) findViewById(R.id.front9hole16)).setText(c.getString(c.getColumnIndex("Hole16"))); 
    ((TextView) findViewById(R.id.front9hole17)).setText(c.getString(c.getColumnIndex("Hole17"))); 
    ((TextView) findViewById(R.id.front9hole18)).setText(c.getString(c.getColumnIndex("Hole18"))); 
    p1 = Integer.parseInt(iden); 
    Cursor c3 = save.rawQuery("Select *from " +data + " where Venue = '"+ courses +"'", null); 
    c3.moveToFirst(); 
    int places = c3.getCount(); 
    if(places == 0){ 
    } else { 
    if(p1 == 111){ 
     Cursor cc = save.rawQuery("Select *from " +data + " where Mobile = '"+ number +"'", null); 
     cc.moveToFirst(); 
     count = cc.getCount(); 
     if(count == 0){ 
     } else { 
     hole1 = cc.getString(cc.getColumnIndex("Hole1")); 
     hole2 = cc.getString(cc.getColumnIndex("Hole2")); 
     hole3 = cc.getString(cc.getColumnIndex("Hole3")); 
     hole4 = cc.getString(cc.getColumnIndex("Hole4")); 
     hole5 = cc.getString(cc.getColumnIndex("Hole5")); 
     hole6 = cc.getString(cc.getColumnIndex("Hole6")); 
     hole7 = cc.getString(cc.getColumnIndex("Hole7")); 
     hole8 = cc.getString(cc.getColumnIndex("Hole8")); 
     hole9 = cc.getString(cc.getColumnIndex("Hole9")); 
     hole10 = cc.getString(cc.getColumnIndex("Hole10")); 
     hole11 = cc.getString(cc.getColumnIndex("Hole11")); 
     hole12 = cc.getString(cc.getColumnIndex("Hole12")); 
     hole13 = cc.getString(cc.getColumnIndex("Hole13")); 
     hole14 = cc.getString(cc.getColumnIndex("Hole14")); 
     hole15 = cc.getString(cc.getColumnIndex("Hole15")); 
     hole16 = cc.getString(cc.getColumnIndex("Hole16")); 
     hole17 = cc.getString(cc.getColumnIndex("Hole17")); 
     hole18 = cc.getString(cc.getColumnIndex("Hole18")); 
     } 
    } else if(p1 == 222) { 
     Cursor cc = save.rawQuery("Select *from " +data + " where Mobile = '"+ number +"'", null); 
     cc.moveToFirst(); 
     count = cc.getCount(); 
     if(count == 0){ 
     } else { 
     hole1 = cc.getString(cc.getColumnIndex("Hole1")); 
     hole2 = cc.getString(cc.getColumnIndex("Hole2")); 
     hole3 = cc.getString(cc.getColumnIndex("Hole3")); 
     hole4 = cc.getString(cc.getColumnIndex("Hole4")); 
     hole5 = cc.getString(cc.getColumnIndex("Hole5")); 
     hole6 = cc.getString(cc.getColumnIndex("Hole6")); 
     hole7 = cc.getString(cc.getColumnIndex("Hole7")); 
     hole8 = cc.getString(cc.getColumnIndex("Hole8")); 
     hole9 = cc.getString(cc.getColumnIndex("Hole9")); 
     hole10 = cc.getString(cc.getColumnIndex("Hole10")); 
     hole11 = cc.getString(cc.getColumnIndex("Hole11")); 
     hole12 = cc.getString(cc.getColumnIndex("Hole12")); 
     hole13 = cc.getString(cc.getColumnIndex("Hole13")); 
     hole14 = cc.getString(cc.getColumnIndex("Hole14")); 
     hole15 = cc.getString(cc.getColumnIndex("Hole15")); 
     hole16 = cc.getString(cc.getColumnIndex("Hole16")); 
     hole17 = cc.getString(cc.getColumnIndex("Hole17")); 
     hole18 = cc.getString(cc.getColumnIndex("Hole18")); 
     } 
    } else if(p1 == 333) { 
     Cursor cc = save.rawQuery("Select *from " +data + " where Mobile = '"+ number +"'", null); 
     cc.moveToFirst(); 
     count = cc.getCount(); 
     if(count == 0){ 
     }else{ 
     hole1 = cc.getString(cc.getColumnIndex("Hole1")); 
     hole2 = cc.getString(cc.getColumnIndex("Hole2")); 
     hole3 = cc.getString(cc.getColumnIndex("Hole3")); 
     hole4 = cc.getString(cc.getColumnIndex("Hole4")); 
     hole5 = cc.getString(cc.getColumnIndex("Hole5")); 
     hole6 = cc.getString(cc.getColumnIndex("Hole6")); 
     hole7 = cc.getString(cc.getColumnIndex("Hole7")); 
     hole8 = cc.getString(cc.getColumnIndex("Hole8")); 
     hole9 = cc.getString(cc.getColumnIndex("Hole9")); 
     hole10 = cc.getString(cc.getColumnIndex("Hole10")); 
     hole11 = cc.getString(cc.getColumnIndex("Hole11")); 
     hole12 = cc.getString(cc.getColumnIndex("Hole12")); 
     hole13 = cc.getString(cc.getColumnIndex("Hole13")); 
     hole14 = cc.getString(cc.getColumnIndex("Hole14")); 
     hole15 = cc.getString(cc.getColumnIndex("Hole15")); 
     hole16 = cc.getString(cc.getColumnIndex("Hole16")); 
     hole17 = cc.getString(cc.getColumnIndex("Hole17")); 
     hole18 = cc.getString(cc.getColumnIndex("Hole18")); 
     } 
    } else if(p1 == 444) { 
     Cursor cc =save.rawQuery("Select *from " +data + " where Mobile = '"+ number +"'", null); 
     cc.moveToFirst(); 
     count = cc.getCount(); 
     if(count == 0){ 
     }else{ 
     hole1 = cc.getString(cc.getColumnIndex("Hole1")); 
     hole2 = cc.getString(cc.getColumnIndex("Hole2")); 
     hole3 = cc.getString(cc.getColumnIndex("Hole3")); 
     hole4 = cc.getString(cc.getColumnIndex("Hole4")); 
     hole5 = cc.getString(cc.getColumnIndex("Hole5")); 
     hole6 = cc.getString(cc.getColumnIndex("Hole6")); 
     hole7 = cc.getString(cc.getColumnIndex("Hole7")); 
     hole8 = cc.getString(cc.getColumnIndex("Hole8")); 
     hole9 = cc.getString(cc.getColumnIndex("Hole9")); 
     hole10 = cc.getString(cc.getColumnIndex("Hole10")); 
     hole11 = cc.getString(cc.getColumnIndex("Hole11")); 
     hole12 = cc.getString(cc.getColumnIndex("Hole12")); 
     hole13 = cc.getString(cc.getColumnIndex("Hole13")); 
     hole14 = cc.getString(cc.getColumnIndex("Hole14")); 
     hole15 = cc.getString(cc.getColumnIndex("Hole15")); 
     hole16 = cc.getString(cc.getColumnIndex("Hole16")); 
     hole17 = cc.getString(cc.getColumnIndex("Hole17")); 
     hole18 = cc.getString(cc.getColumnIndex("Hole18")); 
     } 
    } 
    } 
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
    this, 
    R.layout.spinnerlayout, 
    new String[] { hole1,"1", "2", "3", "4","5","6","7","8","9" }); 
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole2,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole3,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole4,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter5 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole5,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter6 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole6,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter7 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole7,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter7.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter8 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole8,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter8.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter9 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole9,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter9.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter10 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole10,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter10.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter11 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole11,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter11.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter12 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole12,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter12.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter13 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole13,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter13.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter14 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole14,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter14.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter15 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole15,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter15.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter16 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole16,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter16.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter17 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole17,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter17.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     ArrayAdapter<String> adapter18 = new ArrayAdapter<String>(
     this, 
     R.layout.spinnerlayout, 
     new String[] { hole18,"1", "2", "3", "4","5","6","7","8","9" }); 
     adapter18.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     Spinner spinner = (Spinner)this.findViewById(R.id.soloscorehole1front9); 
     spinner.setAdapter(adapter1); 
     ((Spinner) findViewById(R.id.soloscorehole2front9)).setAdapter(adapter2); 
     ((Spinner) findViewById(R.id.soloscorehole3front9)).setAdapter(adapter3); 
     ((Spinner) findViewById(R.id.soloscorehole4front9)).setAdapter(adapter4); 
     ((Spinner) findViewById(R.id.soloscorehole5front9)).setAdapter(adapter5); 
     ((Spinner) findViewById(R.id.soloscorehole6front9)).setAdapter(adapter6); 
     ((Spinner) findViewById(R.id.soloscorehole7front9)).setAdapter(adapter7); 
     ((Spinner) findViewById(R.id.soloscorehole8front9)).setAdapter(adapter8); 
     ((Spinner) findViewById(R.id.soloscorehole9front9)).setAdapter(adapter9); 
     ((Spinner) findViewById(R.id.soloscorehole10front9)).setAdapter(adapter10); 
     ((Spinner) findViewById(R.id.soloscorehole11front9)).setAdapter(adapter11); 
     ((Spinner) findViewById(R.id.soloscorehole12front9)).setAdapter(adapter12); 
     ((Spinner) findViewById(R.id.soloscorehole13front9)).setAdapter(adapter13); 
     ((Spinner) findViewById(R.id.soloscorehole14front9)).setAdapter(adapter14); 
     ((Spinner) findViewById(R.id.soloscorehole15front9)).setAdapter(adapter15); 
     ((Spinner) findViewById(R.id.soloscorehole16front9)).setAdapter(adapter16); 
     ((Spinner) findViewById(R.id.soloscorehole17front9)).setAdapter(adapter17); 
     ((Spinner) findViewById(R.id.soloscorehole18front9)).setAdapter(adapter18); 
     ((Spinner) findViewById(R.id.soloscorehole2front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole3front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole4front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole5front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole6front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole7front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole8front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole9front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole10front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole11front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole12front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole13front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole14front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole15front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole16front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole17front9)).setEnabled(false); 
     ((Spinner) findViewById(R.id.soloscorehole18front9)).setEnabled(false); 
    } 

這是logcat的說:

01-20 17:16:54.742:E/AndroidRuntime(28665):致命異常:#的AsyncTask 1
01-20 17:16:54.742:E/AndroidRuntime(28665):java.lang.RuntimeException:執行doInBackground()時發生錯誤
01-20 17:16:54.742:E/AndroidRuntime(28665) :在
和roid.os.AsyncTask $ 3.done(AsyncTask.java:299)
01-20 17:16:54.742:E/AndroidRuntime(28665):at
java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java :273)
01-20 17:16:54.742:E/AndroidRuntime(28665):at
java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-20 17:16:54.742: E/AndroidRuntime(28665):at
java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:307)
01-20 17:16:54.742:E/AndroidRuntime(28665):at
java.util .concurrent.FutureTask.run(FutureTask.java:137)
01-20 17:16:54.742:E/AndroidRuntime(28665):在
android.os.AsyncTask $ $ SerialExecutor 1.run(AsyncTask.java:230)
01-20 17:16:54.742:E/AndroidRuntime(28665):在
java.util.concurrent.ThreadPoolExecutor.runWorker(的ThreadPoolExecutor。的java:1076)
01-20 17:16:54.742:E/AndroidRuntime(28665):在
java.util.concurrent.ThreadPoolExecutor中$ Worker.run(ThreadPoolExecutor.java:569)
01-20 17:16 :54.742:E/AndroidRuntime(28665):在
java.lang.Thread.run(Thread.java:856)
01-20 17:16:54.742:E/AndroidRuntime(28665):引起:
android。 view.ViewRootImpl $ CalledFromWrongThreadException:只有創建視圖層次結構的原始線程可以觸及其視圖。
01-20 17:16:54.742:E/AndroidRuntime(28665):在
android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607)
01-20 17:16:54.742:E/AndroidRuntime(28665 ):在
android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)

回答

0

因爲你從後臺線程中調用doInBackground()loading()你得到一個錯誤。在loading()中,您正在修改UI的各個方面。除了主用戶界面線程之外,任何線程都不允許這樣做,並且由於您是從後臺線程執行而導致出現異常。

理想情況下,您應該在onPostExecute()中完成所有此UI修改,但您也可以將改變UI的部分包裝在runOnUiThread中。