0

我正在尝试在我的 Adroid 应用程序中使用带有异步任务的进度对话框。但是当我运行我的应用程序时,这会导致致命异常。当我以另一种方法使用它时,它可以正常工作。但是当我用下面的方法使用它时,它导致了一个错误。我在其他问题中读到这是因为数组,但我不知道这是否属实。

这是我的活动代码:

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): 在
android.os.AsyncTask$3.done(AsyncTask.java:299)
01-20 17:16:54.742: E/AndroidRuntime(28665):在
java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-20 17:16:54.742: E/AndroidRuntime(28665): 在
java.util.concurrent.FutureTask.setException(FutureTask.java: 124)
01-20 17:16:54.742: E/AndroidRuntime(28665): 在
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-20 17:16:54.742: E/AndroidRuntime(28665): 在
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): at
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)

4

1 回答 1

0

你得到一个错误,因为你是loading()从后台线程调用的doInBackground()。在loading()中,您正在修改 UI 的各个方面。除了主 UI 线程之外的任何线程都不允许这样做,并且您会得到一个异常,因为您是从后台线程执行此操作的。

理想情况下,您应该将所有这些 UI 修改都onPostExecute()放在runOnUiThread.

于 2013-01-20T09:33:48.303 回答