2011-08-19 153 views
1

我的問題是如何動態地將參數傳遞給AsyncTask。我如何做到這一點?如何將參數傳遞給Android AsyncTask

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.daily_download); 

    SharedPreferences myPrefs = this.getSharedPreferences("myLogedPrefs",MODE_WORLD_READABLE); 
    strBusinessUnit = myPrefs.getString("BusinessUnit", ""); 
    strExecutive = myPrefs.getString("Executive", ""); 
    strTerritoryCode = myPrefs.getString("TerritoryCode", ""); 

    tableListGrid = (GridView)findViewById(R.id.dailydownloadgrid); 
    tableListGrid.setAdapter(new TableListImageAdapter(this)); 
    tableListGrid.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position,long id) { 
      if (position == 0) { 
       dailyDownTable = "LoadTarget"; 
       jsonFilter = "Target1"; 
       new DailyDownldAsyncTask().execute(); 
      } 
     } 
    }); 
} 



    public class DailyDownldAsyncTask extends AsyncTask<Void, Integer, Void> { 
     // private final ProgressDialog dialog = new ProgressDialog(DailyDownloadActivity.this); 
     int myProgress; 

     @Override 
     protected void onPostExecute(Void result) { 

      //if (this.dialog.isShowing()) { 
      // this.dialog.dismiss(); 
      //} 
      //Toast.makeText(DailyDownloadActivity.this,"Download successfully",Toast.LENGTH_SHORT).show(); 

     } 

     @Override 
     protected void onPreExecute() { 

     // this.dialog.setMessage("Downloading Table Data......."); 
     // this.dialog.show(); 
     // myProgress = 0; 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      new DownlaodTableActivity().loadDailyDownloadData(dailyDownTable,jsonFilter,strBusinessUnit, strBusinessUnit,strTerritoryCode); 
     return null; 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 
     // dialog.setProgress(values[0]); 
     } 
} 

我評論「ProgressDialog」 &運行的代碼還我得到了錯誤 這裏loadDailyDownloadDatadailyDownTable,jsonFilter,strBusinessUnit, strBusinessUnit,strTerritoryCode)方法我想通過參數。

當我運行這段代碼我得到錯誤:

 08-19 09:22:15.521: ERROR/AndroidRuntime(457): FATAL EXCEPTION: AsyncTask #2 
08-19 09:22:15.521: ERROR/AndroidRuntime(457): java.lang.RuntimeException: An error occured while executing doInBackground() 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at android.os.AsyncTask$3.done(AsyncTask.java:200) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at java.lang.Thread.run(Thread.java:1019) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at android.os.Handler.<init>(Handler.java:121) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at android.app.Activity.<init>(Activity.java:680) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at com.xont.controller.syn.DownlaodTableActivity.<init>(DownlaodTableActivity.java:62) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at com.xont.controller.admin.DailyDownloadActivity$DailyDownldAsyncTask.doInBackground(DailyDownloadActivity.java:83) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at com.xont.controller.admin.DailyDownloadActivity$DailyDownldAsyncTask.doInBackground(DailyDownloadActivity.java:1) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
08-19 09:22:15.521: ERROR/AndroidRuntime(457):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 

請幫我....

在此先感謝

+0

「DownlaodTableActivity()」 什麼這個...?是一個活動..? – ngesh

回答

5

看來您需要首先閱讀AsyncTask的文檔。 http://developer.android.com/reference/android/os/AsyncTask.html

崩潰問題:

記住doInBackground將在後臺線程中執行。您看到的崩潰問題是DownlaodTableActivity在背景線程內實例化,這在Android框架中是不可能的。 (與參數傳遞無關)所有活動都必須在主UI線程中實例化。如果你已經在UI線程中實例化了Looper.prepare()將被調用。

解決方案:將loadDailyDownloadData移動到不依賴於DownlaodTableActivity的不同類。如果您希望在DownlaodTableActivity中更新UI,請在下載數據後在UI線程中執行此操作。

參數傳遞:

strBusinessUnit = myPrefs.getString("BusinessUnit", ""); 
strExecutive = myPrefs.getString("Executive", ""); 
strTerritoryCode = myPrefs.getString("TerritoryCode", ""); 
... ... 
new DailyDownldAsyncTask().execute(dailyDownTable, jsonFilter, strBusinessUnit, strExecutive, strTerritoryCode); 
... ... 

public class DailyDownldAsyncTask extends AsyncTask<String, Integer, Void> { 
     // private final ProgressDialog dialog = new ProgressDialog(DailyDownloadActivity.this); 
     int myProgress; 

... ... 

     @Override 
     protected Void doInBackground(Void... params) { 
dailyDownloadData(params[0],params[1],params[2], params[3],params[4]; 
     return null; 
     } 
... ... 
} 
+0

謝謝你的回答。我會試試這個 – Piraba

0

你的錯誤列表的第一行說,你的活動不您的活動不在前臺。當您的活動處於前臺時,您的代碼可以正常工作。但在離開活動時會產生此錯誤。

08-19 09:03:51.552: ERROR/AndroidRuntime(340): android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running? 
+0

請告訴我我想做什麼? – Piraba

0

這似乎是錯誤的傳球參數& AsyncTask<Void, Integer, Void>在這裏,您說的void類型....

0

基於對你的函數的名字,這聽起來像你想自動下載東西在後臺使用AsyncTask。這不是AsyncTask的目的 - 它是用於在活動用戶界面的背景下進行操作的。我認爲你真正想使用的是帶有自己線程的服務,並使用由AlarmManager發送的意圖啓動服務。