Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 进度条只加载一次_Java_Android - Fatal编程技术网

Java 进度条只加载一次

Java 进度条只加载一次,java,android,Java,Android,我的进度条只能使用一次,当我第二次点击它时,它就像一个循环,永远不会结束,有什么问题吗 class ProgressThread extends Thread { final static int DONE = 0; final static int RUNNING = 1; int maxBarValue=100; int delay=40; Handler mHandler; int mState; int total;

我的进度条只能使用一次,当我第二次点击它时,它就像一个循环,永远不会结束,有什么问题吗

class ProgressThread extends Thread {   

    final static int DONE = 0;
    final static int RUNNING = 1;
    int maxBarValue=100;
    int delay=40;

    Handler mHandler;
    int mState;
    int total;

    // Constructor with an argument that specifies Handler on main thread
    // to which messages will be sent by this thread.

    ProgressThread(Handler h) {
        mHandler = h;
    }


    @Override
    public void run() {
        mState = RUNNING;   
        total = maxBarValue;
        while (mState == RUNNING) {
            // The method Thread.sleep throws an InterruptedException if Thread.interrupt() 
            // were to be issued while thread is sleeping; the exception must be caught.
            try {
                // Control speed of update (but precision of delay not guaranteed)
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                Log.e("ERROR", "Thread was Interrupted");
            }

            // Send message (with current value of  total as data) to Handler on UI thread
            // so that it can update the progress bar.

            Message msg = mHandler.obtainMessage();
            Bundle b = new Bundle();
            b.putInt("total", total);
            msg.setData(b);
            mHandler.sendMessage(msg);
            total--;    // Count down
        }
        total=maxBarValue;
    }


public class ProgressBarActivity extends Activity {

    ProgressThread progThread;
    ProgressDialog progDialog;
    Button button1, button2;
    int typeBar;                     // Determines type progress bar: 0 = spinner, 1 = horizontal
    int delay = 40;                  // Milliseconds of delay in the update loop
    int maxBarValue = 200;           // Maximum value of horizontal progress bar

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Process button to start spinner progress dialog with anonymous inner class
        button1 = (Button) findViewById(R.id.Button01);
        button1.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                typeBar = 0;
                showDialog(typeBar);
            }
        }); 

        // Process button to start horizontal progress bar dialog with anonymous inner class
        button2 = (Button) findViewById(R.id.Button02);
        button2.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                typeBar = 1;
                showDialog(typeBar);
            }
        }); 
    }

    // Method to create a progress bar dialog of either spinner or horizontal type
    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id) {
        case 0:                      // Spinner
            progDialog = new ProgressDialog(this);
            progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progDialog.setMessage("Loading...");
            progThread = new ProgressThread(handler);
            progThread.start();
            return progDialog;
        case 1:                      // Horizontal
            progDialog = new ProgressDialog(this);
            progDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progDialog.setMax(maxBarValue);
            progDialog.setMessage("Dollars in checking account:");
            progThread = new ProgressThread(handler);
            progThread.start();
            return progDialog;
        default:
            return null;
        }
    }

    // Handler on the main (UI) thread that will receive messages from the 
    // second thread and update the progress.

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            // Get the current value of the variable total from the message data
            // and update the progress bar.
            int total = msg.getData().getInt("total");
            progDialog.setProgress(total);
            if (total <= 0){
                dismissDialog(typeBar);
                progThread.setState(ProgressThread.DONE);
            }
        }
    };
}


    // Set current state of thread (use state=ProgressThread.DONE to stop thread)
    public void setState(int state) {
        state=ProgressThread.DONE;
        mState = state;
    }
}
class ProgressThread扩展线程{
最终静态int DONE=0;
最终静态int运行=1;
int maxBarValue=100;
int延迟=40;
汉德勒;
int mState;
整数合计;
//构造函数,其参数指定主线程上的处理程序
//此线程将向其发送消息。
ProgressThread(处理程序h){
mHandler=h;
}
@凌驾
公开募捐{
mState=正在运行;
总计=最大值;
while(mState==正在运行){
//方法Thread.sleep在Thread.interrupt()时抛出InterruptedException
//将在线程睡眠时发出;必须捕获异常。
试一试{
//控制更新速度(但延迟精度不保证)
睡眠(延迟);
}捕捉(中断异常e){
Log.e(“错误”,“线程被中断”);
}
//向UI线程上的处理程序发送消息(当前值为total作为数据)
//以便它可以更新进度条。
Message msg=mHandler.obtainMessage();
Bundle b=新Bundle();
b、 putInt(“总计”,总计);
msg.setData(b);
mHandler.sendMessage(msg);
总计--;//倒计时
}
总计=最大值;
}
公共课进步活动扩展了活动{
ProgressThread-ProgressThread;
进展对话;
按钮1,按钮2;
int-typeBar;//确定类型进度条:0=微调器,1=水平
int delay=40;//更新循环中的延迟毫秒
int maxBarValue=200;//水平进度条的最大值
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//使用匿名内部类启动微调器进度对话框的进程按钮
button1=(按钮)findViewById(R.id.Button01);
button1.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
typeBar=0;
显示对话框(打印栏);
}
}); 
//使用匿名内部类启动水平进度条对话框的进程按钮
button2=(按钮)findViewById(R.id.Button02);
button2.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
typeBar=1;
显示对话框(打印栏);
}
}); 
}
//方法创建微调器或水平类型的进度条对话框
@凌驾
受保护的对话框onCreateDialog(int id){
开关(id){
案例0://微调器
progDialog=新建ProgressDialog(本);
progDialog.setProgressStyle(ProgressDialog.STYLE_微调器);
progDialog.setMessage(“加载…”);
progThread=新的ProgressThread(处理程序);
progThread.start();
返回程序对话框;
案例1://水平
progDialog=新建ProgressDialog(本);
progDialog.setProgressStyle(ProgressDialog.STYLE_水平);
progDialog.setMax(maxBarValue);
setMessage(“支票账户中的美元:”);
progThread=新的ProgressThread(处理程序);
progThread.start();
返回程序对话框;
违约:
返回null;
}
}
//主(UI)线程上的处理程序,该线程将从
//第二个线程并更新进度。
最终处理程序=新处理程序(){
公共无效handleMessage(消息消息消息){
//从消息数据中获取变量total的当前值
//并更新进度条。
inttotal=msg.getData().getInt(“total”);
progDialog.setProgress(总计);

如果(total你可以试试这个。

根本不能工作。我认为这缺少一些东西。