1

我的 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;

    }
}

但是当我点击按钮时我的应用程序崩溃了,我无法从调试器中找到任何信息。谁能给我一个提示?

提前致谢。

4

5 回答 5

2

您正在创建ProgressDialog两次。从onPreExecute. 另一件事是,在的构造函数中传递 the 的this引用Activity作为contextDeviceScan

import something.something.Logic.*;

public void onClick(View view){

    switch(view.getId())    {

        case R.id.button1:
            new DeviceScan(MyActivity.this).execute("");
      break;

    }
}

onPostExecute另外,按照其他答案的建议更改签名。

于 2012-06-26T10:47:49.310 回答
1

这样做....这样放异步..如果你不明白,请告诉我..问候

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;

ProgressDialog dialog;


public void onClick(View view){


    switch(view.getId())    {

        case R.id.button1:
            new DeviceScan().execute("");
      break;

    }
}
public class DeviceScan extends AsyncTask<String, Void, String> {

protected void onPreExecute() {

      dailog=ProgressDialog.show(classname.this,"Please Wair..","Searching for devices..",false);
}

@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
      */
     dialog.dismiss();
     Toast.makeText(_context, "Done!!", Toast.LENGTH_LONG).show();  
 }
}
于 2012-06-26T10:55:42.823 回答
0

尝试更改onPostExecute(Integer result)onPostExecute(String result).

于 2012-06-26T10:43:42.370 回答
0

无需在上下文中传递。它应该是Activity该类的一个字段,您所做的是从该doInBackground函数创建对话框。

于 2012-06-26T10:44:16.863 回答
0

只是改变它。

 Toast.makeText(_context, "Done!!", Toast.LENGTH_LONG).show(); 
 dialog.dismiss(); 

  dialog.dismiss(); 
  Toast.makeText(_context, "Done!!", Toast.LENGTH_LONG).show(); 

您在dismiss() 对话框之前显示消息。

我认为这会造成问题。

谢谢

于 2012-06-26T10:46:20.123 回答