-1

我试图等到启动 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();

来自原木猫

 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)

我认为它需要等待。希望你能帮忙。

4

2 回答 2

1

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

synchronized(this){
    wait(100);
}
于 2012-09-26T13:53:31.727 回答
0

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

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

于 2012-09-26T13:43:52.573 回答