强制关闭AsyncTask

时间:2012-09-26 13:22:10

标签: android android-asynctask splash-screen

我试图等到启动AsyncTask从服务器获取结果,因此主要活动将继续,在我的启动画面上我有一些带加载进程条的GUI等等。

但是当我跑步时,我会强行关闭。 这是我的主要活动代码:

 lv.setOnItemClickListener(new OnItemClickListener() 
 {
     public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
     new Thread() { public void run() 
     {
         runOnUiThread(new Runnable() {
             public void run() {
                 splash  splash=(tools.splash) new splash(first.this).execute();
                 int waited = 0;
                 while(splash.running && (waited< getResources().getInteger(R.integer.splashTimeOut)))
                 {
                     try {
                         wait(100);
                         } catch (InterruptedException e) {
                             // TODO Auto-generated catch block
                             e.printStackTrace();
                         }
                         if(splash.running) {
                             waited += 100;
                         }
                         // nextActivity=splash.newActivity;
                     }
                     Intent intent = null;
                     try {
                         intent = new Intent(first.this,Class.forName("activities.third"));
                     } catch (ClassNotFoundException e) {
                         // TODD Auto-generated catch block
                         e.printStackTrace();
                     }
                     startActivity(intent);
                 }

              });
          }
      }.start();
来自log cat的

 09-26 13:41:26.862: ERROR/AndroidRuntime(487): java.lang.IllegalMonitorStateException: object not locked by thread before wait()
  09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at java.lang.Object.wait(Native Method)
 09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at java.lang.Object.wait(Object.java:395)
 09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at activities.first$1$1$1.run(first.java:54)
 09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at android.os.Handler.handleCallback(Handler.java:587)
09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at android.os.Handler.dispatchMessage(Handler.java:92)
 09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at android.os.Looper.loop(Looper.java:123)
 09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at android.app.ActivityThread.main(ActivityThread.java:3683)
  09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at java.lang.reflect.Method.invokeNative(Native Method)
 09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at java.lang.reflect.Method.invoke(Method.java:507)
09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-26 13:41:26.862: ERROR/AndroidRuntime(487):     at dalvik.system.NativeStart.main(Native Method)

我觉得它有点等待。 希望你能提供帮助。

2 个答案:

答案 0 :(得分:1)

您需要与正在调用wait()的对象同步。在这种情况下,您应该可以说

synchronized(this){
    wait(100);
}

答案 1 :(得分:0)

runOnUiThread在UI线程上运行。您无权通过调用连续等待(100);

来阻止它

此外,您的代码中没有Asynctask ......