2

我正在尝试使用线程显示进度条。我接受我没有那么多线程概念。

这是代码

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();
    }
}

运行时,logcat 消息是

java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序

我在这里做错了什么?

4

3 回答 3

3

所以你的问题将在其他地方。我用 Handler 尝试了你的例子,它对我有用。

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();         

        }
    }

和 XML:

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

因此,请查看并编辑与此类似的代码。

于 2012-05-28T09:36:20.423 回答
1

找到以下使用线程更新进度条的示例代码

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

public class ThreadDemo1ProgressBar extends Activity
{
    ProgressBar bar;
    TextView msgWorking;
    boolean isRunning = false;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            bar.incrementProgressBy(5);
            if (bar.getProgress() == bar.getMax()) {
                msgWorking.setText("Done");
                bar.setVisibility(View.INVISIBLE);
            } else {
                msgWorking.setText("Working..." +
                        bar.getProgress());
            }
        }// handleMessage
    };

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        bar = (ProgressBar) findViewById(R.id.progress);
        bar.setMax(100);
        msgWorking = (TextView) findViewById(R.id.TextView01);
    }

    public void onStart() {
        super.onStart();
        bar.setProgress(0);

        Thread background = new Thread(new Runnable() {
            public void run() {
                try
                {
                    for (int i = 0; i < 20 && isRunning; i++) {

                        Thread.sleep(1000);
                                handler.sendMessage(handler.obtainMessage());
                    }
                }
                catch(Throwable t) {
                    // just end the background thread
                }
            }
        });
        isRunning = true;
        background.start();
    }// onStart

    public void onStop() {
        super.onStop();
        isRunning = false;
    }
}// ThreadDemo1ProgressBar

about 示例每 5 秒更新一次进度条。

于 2012-05-28T09:47:18.423 回答
0

我实际上只创建了一次线程实例,它仍然可以工作。此代码是在启动活动中编写的。您需要做的就是调用showSpinner1()方法来显示/隐藏微调器。

确保 getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);在您的onCreate()方法中执行此操作,并使用此代码打开和关闭微调器。

// 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);
}
于 2012-05-28T10:00:46.043 回答