Android Async Task Progress Bar onProgressUpdate

前端 未结 3 936
广开言路
广开言路 2020-12-30 16:50

I am a new programmer. I have seen lots of tutorials but can\'t understand what is wrong. I am trying to create a ProgressBar from an Async Task. However it always crashes m

相关标签:
3条回答
  • 2020-12-30 17:33
    Intervals.getApp().progressBar.setProgress(values[0]);
    

    There are two possibilities for the NullPointerException in this line of code:

    1. getApp() returns null
    2. progressBar is null

    In order to determine which is the cause, you need to split this into two separate lines:

    Intervals intervals = Intervales.getApp();
    intervals.progressBar.setProgress(values[0]);
    

    Now set a breakpoint at the first line, step over it, and determine if intervals is null or not.

    0 讨论(0)
  • 2020-12-30 17:42

    progressBar is a local variable while you are referring to the same-named variable of a different class-object (in your case, the app object). you have probably mixed them up.

    0 讨论(0)
  • 2020-12-30 17:51

    I think it's better if you pass the progress bar as an argument to the AsyncTask:

    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.Progressbar);
    progressBar.setProgress(0);
    
    // Play Methods
    final ImageButton Play = (ImageButton) findViewById(R.id.Play);
    Play.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View view) {
            AsyncTaskBar task = new AsyncTaskBar();
            task.setProgressBar(progressBar);
            task.execute();
        }
    

    And then on your AsyncTask declare the setProgressBar method:

    public class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void> {
    
    ProgressBar bar;
    
    public void setProgressBar(ProgressBar bar) {
        this.bar = bar;
    }
    
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        if (this.bar != null) {
            bar.setProgress(values[0]);
        }
    }
    

    You could do the same with the TextView you're trying to set.

    In any case, since you mentioned you're new at this you may want to take a look at the Broadcast/Receiver pattern. Here's how it goes:

    1. Start the async task without setting the progress bar or anything.
    2. Define a BroadcastReceiver, instantiate one in your activity and register/unregister it accordingly.
    3. Whenever there's a progress update on your async task just call sendBroadcast with the progress update as an intent extra. You may need to pass a context parameter when instantiating the AsyncTask.
    4. The onHandleIntent method of your app's broadcast receiver (the one you instantiated on step 2) will run on the UI thread, making all those UI updates safe.

    Sounds a bit overwhelming? It is at first, but here are the benefits:

    1. It is much cleaner than passing UI objects to an AsyncTask.
    2. You will learn a powerful Android pattern that will come in handy in other endeavours.
    3. It will save you a lot of hassle (and exceptions) if you switch context or your app is low on memory.
    0 讨论(0)
提交回复
热议问题