0

It is my first time implement horizontal progress bar in my application The goal is after the progress bar finish, the application show toast message and image bitmap the toast message is doing fine but the image make the application to force close

this is my code:

Button buttonProceed = (Button) findViewById(R.id.buttonProceed);
    buttonProceed.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(true);
            progressBar.setMessage("Processing");
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
            progressBar.setMax(100);
            progressBar.show();

            progressBarStatus = 0;

            fileProcess = 0;

            String toastMessage = "Citra host berhasil ditanam citra watermark dan disimpan di internal storage";
            final Toast toast = Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);

            final ImageView resultImageView = (ImageView) findViewById(R.id.viewResult);

            new Thread(new Runnable() {
                public void run() {
                    while (progressBarStatus < 100) {
                        progressBarStatus = progressMarker();

                        try{
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        progressBarHandler.post(new Runnable() {
                            public void run() {
                                progressBar.setProgress(progressBarStatus);
                            }
                        });
                    }

                    if (progressBarStatus >= 100) {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        progressBar.dismiss();
                        toast.show();
                        //the resultBitmap variable is declared in global
                        resultImageView.setImageBitmap(resultBitmap);
                    }
                }
            }).start();
        }
    });
}

So, could someone help me figure what wrong in my code?

Regard!

Haryanto
  • 43
  • 2
  • 2
  • 7
  • If you want to do process in background then [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) is best for it. – Dhruv Nov 30 '14 at 18:36

1 Answers1

0

The problem is that you aren't allowed to change UI in new Thread. For this purpose you could use AsyncTask instead of Thread.

Here is some example: How to use asynctask to display a progress bar that counts down?

You should call progressBar.setProgress(progressBarStatus); in onProgressUpdate(String... progress) of AsyncTask.

And this code should be called in onPostExecute(String result):

progressBar.dismiss();
toast.show();
//the resultBitmap variable is declared in global
resultImageView.setImageBitmap(resultBitmap);

Because code in onPostExecute is called on main UI Thread.

Community
  • 1
  • 1
vzoha
  • 151
  • 5