2016-07-25 149 views
-1

致命異常:AsyncTask#2在我的應用程序中。有人可以告訴我爲什麼,並告訴我我需要做什麼。我現在很困惑,需要一點幫助。致命異常asynctask#2 AndroidRuntime

這裏是我的代碼

Bundle b = getIntent().getExtras(); 
    pDialog = new ProgressDialog(DataActivity.this); 
    Id = b.getString("Id"); 
    Exists = b.getBoolean("Exists"); 

    final String myUrl = "http://xxxxxxxxxx/api.php?id="+Id; 
    try 
    { 
     new MyAsyncTaskGetNews().execute(myUrl,"false"); 
    }catch (Exception e){Log.d("d2",e.toString());} 


    IB = (ImageButton)findViewById(R.id.imageButton); 
    IB.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 

      try 
      { 
       new MyAsyncTaskGetNews().execute(myUrl,"true"); 
      }catch (Exception e){Log.d("d1",e.toString());} 
     } 
    }); 

和logcat的

07-25 19:47:25.393 8717-9017/com.xxxxxxxx.xxxx E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2 
                   Process: com.xxxxxxxx.xxxx, PID: 8717 
                   java.lang.RuntimeException: An error occurred while executing doInBackground() 
                    at android.os.AsyncTask$3.done(AsyncTask.java:309) 
                    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) 
                    at java.util.concurrent.FutureTask.setException(FutureTask.java:223) 
                    at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                    at java.lang.Thread.run(Thread.java:818) 
                   Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
                    at android.os.Handler.<init>(Handler.java:200) 
                    at android.os.Handler.<init>(Handler.java:114) 
                    at android.widget.Toast$TN.<init>(Toast.java:359) 
                    at android.widget.Toast.<init>(Toast.java:105) 
                    at android.widget.Toast.makeText(Toast.java:266) 
                    at com.xxxxxxxx.xxxx.DataActivity$MyAsyncTaskGetNews.doInBackground(DataActivity.java:136) 
                    at com.xxxxxxxx.xxxx.DataActivity$MyAsyncTaskGetNews.doInBackground(DataActivity.java:93) 
                    at android.os.AsyncTask$2.call(AsyncTask.java:295) 
                    at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)  
                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)  
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)  
                    at 

java.lang.Thread.run(Thread.java:818)  
07-25 19:47:25.533 8717-8717/com.xxxxxxxx.xxxx I/Timeline: Timeline: Activity_idle id: [email protected] time:35310450 
07-25 19:47:25.705 8717-8717/com.xxxxxxxx.xxxx E/WindowManager: android.view.WindowLeaked: Activity com.xxxxxxxx.xxxx.DataActivity has leaked window com.android.internal.policy.PhoneWindow$DecorView{afa2ab V.E...... R......D 0,0-1026,483} that was originally added here 
                     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:372) 
                     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299) 
                     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:86) 
                     at android.app.Dialog.show(Dialog.java:326) 
                     at com.xxxxxxxx.xxxx.DataActivity$MyAsyncTaskGetNews.onPreExecute(DataActivity.java:105) 
                     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:604) 
                     at android.os.AsyncTask.execute(AsyncTask.java:551) 
                     at com.xxxxxxxx.xxxx.DataActivity$1.onClick(DataActivity.java:87) 
                     at android.view.View.performClick(View.java:5280) 
                     at android.view.View$PerformClick.run(View.java:21239) 
                     at android.os.Handler.handleCallback(Handler.java:739) 
                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                     at android.os.Looper.loop(Looper.java:224) 
                     at android.app.ActivityThread.main(ActivityThread.java:5526) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+0

爲了幫助解決例外問題,我們需要看到AsyncTask類 –

回答

0

你的異步任務GetNews正在打造一個doInBackground處理程序。它不能那樣做。如果需要創建一個Handler來發布到UI線程,它應該在onPreExecute中完成。但是真正的AsyncTask應該永遠不需要這麼做,它應該使用onPostExecute和publishProgress/onProgress來避免需要這樣做。

+0

我不明白 –

+0

您不能在線程上創建新的處理程序,例如AsyncTask的doInBackground。它必須在UI線程或調用Looper.loop()的線程上創建。所以你在錯誤的地方創建了Handler。然而機會是好的,你根本不需要Handler,並且有更好的方法來做你想做的事情。 –

相關問題