AsyncTask类在onProgressUpdate方法中显示空指针异常

时间:2013-06-21 11:13:36

标签: android android-asynctask

我是android的初学者。我试图在扩展android的内置信使类的类中使用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 个答案:

答案 0 :(得分:3)

由于您无法在服务中找到ViewById,请在启动服务的活动中执行相同的操作,并将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);

        }

希望这有效。

答案 1 :(得分:2)

您永远不会初始化bar,因此在bar.setProgress(values[0])使用它时它为空。

答案 2 :(得分: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方法中使用dialog对象。

编辑:

通过Binder,您可以向您的活动发送回调,这意味着您可以像进度对话框一样更新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)

答案 3 :(得分:0)

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