Kumar
Kumar

Reputation: 169

displaying progressbar using threading

I am trying to display a progress bar using threading .. I accept that I do not have that much concept of threading.

Here is the code

public class Progress extends Activity {

    static String[] display;
    private static final int Progress = 0;      
    private ProgressBar mProgress;
    private int mProgressStatus = 0;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress);

        mProgress = (ProgressBar) findViewById(R.id.progressBar);

        // Start lengthy operation in a background thread
        new Thread(new Runnable() {
            public void run() {
                while (mProgressStatus < 100) {
                    mProgressStatus = doWork();

                    // Update the progress bar
                    mHandler.post(new Runnable() {
                        public void run() {
                            mProgress.setProgress(mProgressStatus);
                        }
                    });
                }
            }

            private int doWork() {                  
                display = new Logic().finaldata();
                // TODO Auto-generated method stub
                return 100;
            }
        }).start();
    }
}

On running, the logcat message is

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

What is the mistake that I am doing here?

Upvotes: 2

Views: 24310

Answers (2)

Simon Dorociak
Simon Dorociak

Reputation: 33515

So your problem will be elsewhere. I tried your example with Handler and it works for me.

package com.sajmon.threadsDemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
import android.widget.TextView;

    public class ThreadsDemoActivity extends Activity {

        ProgressBar bar;
        TextView label;
        Handler handler = new Handler();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            bar = (ProgressBar) findViewById(R.id.progBar);

            new Thread(new Runnable() {

                int i = 0;
                int progressStatus = 0;

                public void run() {
                    while (progressStatus < 100) {
                        progressStatus += doWork();
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        // Update the progress bar
                        handler.post(new Runnable() {
                            public void run() {
                                bar.setProgress(progressStatus);
                                i++;
                            }
                        });
                    }
                }
                private int doWork() {

                    return i * 3;
                 }

                }).start();         

        }
    }

And XML:

<ProgressBar 
        android:id="@+id/progBar" style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

So look at this and edit your code similar with this.

Upvotes: 3

Swaroop
Swaroop

Reputation: 916

I actually just create a thread instance once and it works anyway. This code was written in the Startup Activity. All you need to do is call showSpinner1() method to show/hide the spinner.

Ensure to do this getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS); in your onCreate() method and use this code for toggling the spinner ON and OFF.

// Spinner related code - The thread is created just once and is used multiple times (works!!)
boolean toShow = false;
Thread spinner1Thread = new Thread("Show/Hide Spinner Thread") {

    @Override
    public void run() {
        setProgressBarIndeterminateVisibility(toShow);
    }

};

/**
 * Shows and hides the spinner
 * @param pShow
 */
public void showSpinner1(boolean pShow) {
    toShow = pShow;
    runOnUiThread(spinner1Thread);
}

Upvotes: 0

Related Questions