我的問題是如何動態地將參數傳遞給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)
請幫我....
在此先感謝
「DownlaodTableActivity()」 什麼這個...?是一個活動..? – ngesh