-1

我是android的初学者。我正在尝试在扩展 android 的内置 messenger 类的类中使用 AsyncTask 控制进度条。我收到一个异常,但无法理解我的代码中的错误。

public class MyMessenger extends Service {

private ProgressDialog downloadProgressDialog;
static final int v1=1,v2=2;
ProgressBar bar;
class MyHandler extends Handler
{
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case v1:

            Toast.makeText(getApplicationContext(), "message = 1 in handler of messenger", Toast.LENGTH_LONG).show();
            break;
        case v2:
            new sync().execute();
                        break;

        }
    }
}
Messenger messenger=new Messenger(new MyHandler());
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    //Toast.makeText(getApplicationContext(), "binding...", Toast.LENGTH_LONG).show();
    return messenger.getBinder();

}
public class sync extends AsyncTask<Void, Integer, Void>
{
    int progress=0;
    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
    //  super.onProgressUpdate(values);

        downloadProgressDialog.setProgress(values[0]);
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        while (progress<100) {
            progress++;
            publishProgress(progress);
            SystemClock.sleep(1000);
        }
        return null;
    }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        downloadProgressDialog = new ProgressDialog(getApplicationContext());
        downloadProgressDialog.setMessage("Downloading file...");
        downloadProgressDialog.setMax(100);
        downloadProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        downloadProgressDialog.setCancelable(false);
        downloadProgressDialog.show();
    }

}

}

4

4 回答 4

3

由于您无法在 service 中 findViewById ,因此请在启动服务的活动中执行相同操作,并将 bar 作为公共静态变量。bar 为 null 因为您尚未通过 findViewById 对其进行初始化

public static ProgressBar bar =  (ProgressBar) findViewById(R.id.progressbar);//in activity

异步任务中的代码

//async task
     @Override
        protected void onProgressUpdate(Integer... values) {
          if(!MyActivityName.bar=null)

             MyActivityName.bar.setProgress(values[0]);
             super.onProgressUpdate(values);

        }

希望这有效。

于 2013-06-21T11:48:19.073 回答
2

你从不初始化bar,所以在你使用它时它是空的bar.setProgress(values[0])

于 2013-06-21T11:14:25.013 回答
1

因为你没有初始化你的进度条。初始化如下:

bar =  (ProgressBar) findViewById(R.id.progressbar);

编辑:

然后你必须使用 Progressdialog 而不是像下面这样的进度条:

private ProgressDialog downloadProgressDialog;

protected void onPreExecute() {
        super.onPreExecute();

       downloadProgressDialog = new ProgressDialog(context);
            downloadProgressDialog.setMessage("Downloading file...");
            downloadProgressDialog.setMax(100);
            downloadProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            downloadProgressDialog.setCancelable(false);
            downloadProgressDialog.show();
    }

然后在你的 progressupdate 方法中使用对话框对象。

编辑:

通过 Binder,您可以向 Activity 发送回调,这意味着您可以像进度对话框一样更新 UI。

Add according method to your Binder (let's name it onProgress)
From your AsyncTask call method of this Binder
In order to know about progress updates consider using Observer pattern (in other words - your Activity should listen for updates of your Binder, or more specifically - of calling Binder.onProgress method)
于 2013-06-21T11:17:56.720 回答
0

您不能显示进度对话框,您可以扩展用于在后台运行线程的服务类。只使用 doinbackground() 方法,删除进度对话框方法。

于 2013-06-21T11:32:10.343 回答