我的 AsyncTask 中有以下代码。我希望 AsyncTask 做的唯一一件事就是休眠 1000 毫秒,同时显示一个 ProgressDialog。
package something.something.Logic;
import android.R;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class DeviceScan extends AsyncTask<String, Void, String> {
ProgressDialog dialog;
Context _context;
public DeviceScan(Context context) {
_context = context;
dialog = new ProgressDialog(_context);
}
protected void onPreExecute() {
dialog = new ProgressDialog(_context);
dialog.setTitle("Please Wait");
dialog.setMessage("Searching for devices..");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected String doInBackground(String... params) {
for(int i=0;i<5;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "";
}
protected void onPostExecute(Integer result) {
/*
* When the background thread is finished, do something here
*/
Toast.makeText(_context, "Done!!", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}
然后我以这种方式调用 AsyncTask:
import something.something.Logic.*;
public void onClick(View view){
switch(view.getId()) {
case R.id.button1:
new DeviceScan(getApplicationContext()).execute("");
break;
}
}
但是当我点击按钮时我的应用程序崩溃了,我无法从调试器中找到任何信息。谁能给我一个提示?
提前致谢。