2014-11-08 62 views
0

嗨我想在android中實現一個通用的Asynctask。我有大約8到10個活動,它使用具有不同參數的相同的asynctask。任務適用於每項活動。但是當我嘗試實現UI組件時,它會崩潰。這裏是我的代碼有什麼建議多種活動的通用Asynctask

public class SyncTaskService extends AsyncTask<String, Void, Void> { 
    OnAsyncResult onAsyncResult; 
    String ServerResponse; 
    String LocalResponse; 
    int Operationtype=-1; 

    private Context mContext; 
    public SyncTaskService (Context context){ 
     mContext = context; 
    } 

    public void setOnResultListener(OnAsyncResult onAsyncResult) { 
     if (onAsyncResult != null) { 
      this.onAsyncResult = onAsyncResult; 
     } 
    } 

    @Override 
    protected Void doInBackground(String... params) { 
     BackGroundTasks Obj_BackGroungTasks = new BackGroundTasks(); 
     Operationtype = Integer.parseInt(params[0]); 
     if (onAsyncResult != null) 
     { 
      switch (Operationtype) { 
      case 1: 
       String Type = params[2]; 
       ServerResponse=null; 
       ServerResponse= Obj_BackGroungTasks.getdetails(mContext,Type); 
       break; 
      default: 
       break; 
      } 
     } 
     if (ServerResponse!=null) { 
      onAsyncResult.onResultSuccess(ServerResponse); 
     } else { 
      onAsyncResult.onResultFail(ServerResponse +LocalResponse); 
     } 
     return null; 
    } 
    public interface OnAsyncResult { 
     public abstract void onResultSuccess(String message); 
     public abstract void onResultFail(String errorMessage); 
    } 

,這是我如何使用它在我的活動

SyncTaskService task = new SyncTaskService(getActivity()); 
    task.setOnResultListener(asynResult); 
    task.execute("5",promotype); 


OnAsyncResult asynResult = new OnAsyncResult() { 
     @Override 
     public void onResultFail(final String errorMessage) { 
      Log.d("On Result Fail In Activity", ""+errorMessage); 
      Toast.makeText(getActivity(), "Task completed", Toast.LENGTH_LONG).show(); 

     } 
     @Override 
     public void onResultSuccess(String message) { 
      Log.d("On Result Success In Activity", ""+message); 
      ServerResponse=message; 
      Toast.makeText(getActivity(), "Task completed", Toast.LENGTH_LONG).show(); //craches here 
     } 


     }; 

這裏我們logcat的

11-08 16:25:17.705: E/AndroidRuntime(671): FATAL EXCEPTION: AsyncTask #1 
11-08 16:25:17.705: E/AndroidRuntime(671): java.lang.RuntimeException: An error occured while executing doInBackground() 
11-08 16:25:17.705: E/AndroidRuntime(671): at android.os.AsyncTask$3.done(AsyncTask.java:278) 
11-08 16:25:17.705: E/AndroidRuntime(671): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
11-08 16:25:17.705: E/AndroidRuntime(671): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
11-08 16:25:17.705: E/AndroidRuntime(671): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
11-08 16:25:17.705: E/AndroidRuntime(671): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
11-08 16:25:17.705: E/AndroidRuntime(671): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
11-08 16:25:17.705: E/AndroidRuntime(671): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
11-08 16:25:17.705: E/AndroidRuntime(671): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
11-08 16:25:17.705: E/AndroidRuntime(671): at java.lang.Thread.run(Thread.java:856) 
11-08 16:25:17.705: E/AndroidRuntime(671): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
11-08 16:25:17.705: E/AndroidRuntime(671): at android.os.Handler.<init>(Handler.java:121) 
11-08 16:25:17.705: E/AndroidRuntime(671): at android.widget.Toast$TN.<init>(Toast.java:317) 
11-08 16:25:17.705: E/AndroidRuntime(671): at android.widget.Toast.<init>(Toast.java:91) 
11-08 16:25:17.705: E/AndroidRuntime(671): at android.widget.Toast.makeText(Toast.java:233) 
11-08 16:25:17.705: E/AndroidRuntime(671): at packagename.Activity$1.onResultFail(Activity.java:157) 
11-08 16:25:17.705: E/AndroidRuntime(671): at packagename.SyncTaskService.doInBackground(SyncTaskService.java:115) 
11-08 16:25:17.705: E/AndroidRuntime(671): at packagename.SyncTaskService.doInBackground(SyncTaskService.java:1) 
11-08 16:25:17.705: E/AndroidRuntime(671): at android.os.AsyncTask$2.call(AsyncTask.java:264) 
11-08 16:25:17.705: E/AndroidRuntime(671): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
11-08 16:25:17.705: E/AndroidRuntime(671): ... 5 more 
+0

您嘗試訪問UI線程在異步任務 – Amy 2014-11-08 11:41:47

+0

所以我應該在哪裏做我的處理一旦我在ServerResponse串 – DreamCoder 2014-11-08 11:43:15

+0

onPostExcecute方法 – Amy 2014-11-08 11:44:02

回答

0

你需要重寫onPostExecuted()方法。基本上,的AsyncTask有4種重要的方法,其中三個執行UI線程和其中一個執行後臺異步線程:

  1. onPreExecute() - UI線程,你應該之前,做任何你想要在UI上執行你的後臺進程執行前。顯示一個進度條
  2. doInBackground() - 在後臺線程上執行,這是你應該做任何繁重的任務需要做的關閉UI線程,以便您的應用程序不會崩潰或您的用戶界面不會掛死前。進行API調用以下載。請記住,您無法與此處的任何UI元素交互!你也可以從這裏發佈進度。返回文件大小每200kb下載一次,以便進度條可以更新
  3. onPostExecute() - 在UI線程上執行,您應該在任務執行完畢後處理UI上要做的所有事情。關閉進度條並顯示下載文件的結果
  4. onProgressUpdate() - 在UI線程上執行時doInBackground()發佈進度,以便在出現長時間後臺進程的情況下保持用戶更新。在這裏更新你的進度欄百分比

希望這會有所幫助。您可能還想看看可能幫助您進一步瞭解Asynctask全部功能的文檔。

https://developer.android.com/reference/android/os/AsyncTask.html

+0

謝謝Qazi,但我已經使用了RunOnUiThread方法,並且對我很好。 – DreamCoder 2014-11-10 21:06:48