读书人

单击Button,控制进度条更新,该如何处理

发布时间: 2012-02-24 16:30:38 作者: rapoo

单击Button,控制进度条更新
预备实现的效果:通过单击Button,暂停进度条(.suspend())更新或者进度条继续重新开始刷新(.resume())。

目前程序一点击Button,出现错误:
Uncaught handler:thread main exiting due to uncaught exception。

各位大大,通过单击Button,暂停进度条(.suspend())更新或者进度条继续重新开始刷新(.resume())。逻辑是否正确呢?或者采用其他什么api实现呢?
下面的程序怎么会报错呢,不明白,谢谢!

Java code
        /*     * Button listener action:@hRunBootTestListener(View button)     * suspend and resume detected device action by button text value.     */    private void hRunBootTestListener(View button) {        switch(button.getId()){        case R.id.Pause:{            if(pauseText){                mPauseButton.setText("Start");                pauseText=false;            }else if(!pauseText){                mPauseButton.setText("Pause");                pauseText=true;            }               /*              * Expected result :             * Click pause button ,mThread suspend.             * Click start button ,mThread resume.             */            if(pauseText){                Log.i("mThread resume1", "nThread resume1");                Log.i("resume pauseText value is ", mPauseButton.getText().toString());                mThread.resume();            }else{                Log.i("mThread suspend1", "nThread suspend1");                Log.i("suspend pause value is", mPauseButton.getText().toString());                mThread.suspend();            }                    }break;        case R.id.Cancel:{            Log.i("Cancel button is clicked","cancle button");        }break;        }            }    


文件完整代码:
Java code
import java.util.Timer;import java.util.TimerTask;import wistron.mojo.R;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.graphics.Typeface;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ProgressBar;import android.widget.TextView;import android.widget.Toast;/* * After the countdowntime has expired,start detectedd device,at the same time display the progress. * and update data to SETTING_Infos.xml file. */public class UpdateProgress extends Activity implements OnClickListener{    public static final String SETTING_Infos = "SETTING_Infos";    public static final String BootNum = "BootNum";    public static final String SleepTime = "SleepTime";    public static final String mPowerType="coldboot";    public static final String Successful_time="0";    private final static int TRACK_TEXTVIEW = 0;    private final static int TRACK_PROGRESSBAR = 1;            public int mSleepTime,mBootNum,mSuccessful_time;    public int mSuccessfulBoot,mPowerCycleType;            public Button mPauseButton,mCancelButton;        public EditText mSuccessfulBootEdit,mCountdownSleepEdit;    public TextView SleepTimeTV,BootNumTV,CountDownTV;    public TextView tv;    public ProgressBar mProgressusb,mProgressssd,pb;    public Timer timer;    Thread mThread;        public boolean isRunning = false;    boolean pauseText=true;        private final int[][] mTrackSlotResource = {            {R.id.txviewssd, R.id.progressssd},            {R.id.txviewusb, R.id.progressusb},    };                public void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.progress);            InitialUI();    }    /*     * Initial UI:     * Read from sharedPreference -- @GetSharedPreferences();      * Sleep -- @CountDownTime();             * @InitialProgress();         */    private void InitialUI() {        findView();        SetListener();        GetSharedPreferences();            CountDownTime();    }        private void findView() {        mPauseButton = (Button) findViewById(R.id.Pause);        mCancelButton = (Button) findViewById(R.id.Cancel);            BootNumTV=(TextView)findViewById(R.id.BoottimesTV);        CountDownTV = (TextView) findViewById(R.id.CountdownTimeTV);    }    private void SetListener() {        mPauseButton.setOnClickListener(this);        mCancelButton.setOnClickListener(this);    }        private void GetSharedPreferences() {                    SharedPreferences settings = getSharedPreferences(SETTING_Infos, 0);        mBootNum=settings.getInt(BootNum,1);        mSleepTime=settings.getInt(SleepTime,1);        mSuccessful_time=settings.getInt(Successful_time, 0);        BootNumTV.setText("BootNum is"+String.valueOf(mBootNum));    }        /*     * Button listener action:@hRunBootTestListener(View button)     * suspend and resume detected device action by button text value.     */    private void hRunBootTestListener(View button) {        switch(button.getId()){        case R.id.Pause:{            if(pauseText){                mPauseButton.setText("Start");                pauseText=false;            }else if(!pauseText){                mPauseButton.setText("Pause");                pauseText=true;            }               /*              * Expected result :             * Click pause button ,mThread suspend.             * Click start button ,mThread resume.             */            if(pauseText){                Log.i("mThread resume1", "nThread resume1");                Log.i("resume pauseText value is ", mPauseButton.getText().toString());                mThread.resume();            }else{                Log.i("mThread suspend1", "nThread suspend1");                Log.i("suspend pause value is", mPauseButton.getText().toString());                mThread.suspend();            }                    }break;        case R.id.Cancel:{            Log.i("Cancel button is clicked","cancle button");        }break;        }            }                /*     * @handler:Receives a different message value through handling of incidents     * @CountDownTime() message.what=1     * @InitialProgress() message.what=2      */    final Handler handler = new Handler(){           public void handleMessage(Message msg) {              switch (msg.what) {             case 1:                                CountDownTV.setText(getCountdownString());                     break;             case 2:                    for(int m=0;m<2;m++){                        tv = (TextView) findViewById(mTrackSlotResource[m][TRACK_TEXTVIEW]);                        pb = (ProgressBar) findViewById(mTrackSlotResource[m][TRACK_PROGRESSBAR]);                                             int progress=pb.getProgress();                                        if(progress==pb.getMax()){                            tv.setText("Checking is completed ");                        }else if(progress>(3*(pb.getMax())/4)){                            tv.setText("Checking completed 3/4..... ");                        }else if(progress>(2*(pb.getMax())/4)){                            tv.setText("Checking usb completed 2/4..... ");                         }else if(progress>((pb.getMax())/4)){                            tv.setText("Checking completed 1/4..... ");                        }else{                            tv.setText("Checking is going..... ");                         }                            pb.incrementProgressBy(5);                    }                            break;                 }              super.handleMessage(msg);          }     };    /*     * @InitialProgress()     * Initial Progressbar and sleep 1s every time     */    private void InitialProgress() {         mThread = new Thread(new Runnable() {            @Override            public void run() {                Message message = new Message();                      message.what = 2;                for(int i=0;i<30&& isRunning;i++){                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                    }                      }            }          });       }        /*     * @CountDownTime() : CountDownTime and display on UpdateProgress UI     * This class includ Three method:      * @CountDownTime()     * TimerTask task      * @getCountdownString():format string     */    private void CountDownTime() {        Typeface font = Typeface.createFromAsset(this.getAssets(),                "fonts/digital-7 (mono).ttf");        CountDownTV.setTypeface(font);                    timer = new Timer("minutes-to-midnight");                timer.schedule(task,1000, 1000); //delay 1000ms excute,anmout of 1000ms in between subsequent executions        /*         * Display message "Start detected device" of toast         */        Context context = getApplicationContext();        CharSequence text = "After"+mSleepTime+"s"+"Start detected device";        int duration = Toast.LENGTH_SHORT;             Toast toast = Toast.makeText(context, text, duration);        toast.show();                           }        TimerTask task = new TimerTask(){              public void run() {              Message message = new Message();                  message.what = 1;            --mSleepTime;            if(mSleepTime >= 0) {                handler.sendMessage(handler.obtainMessage(1));            }            if(mSleepTime<0){                handler.sendMessage(handler.obtainMessage(2));                    isRunning = true;                   mThread.start();                }        }      };     private String getCountdownString(){        int hour=00;        int minute=00;        int second = mSleepTime;        return String.format("%02d:%02d:%02d", hour, minute, second);        }        @Override    public void onClick(View button) {        hRunBootTestListener(button);            }    } 



[解决办法]
在错误之后应该还有具体的Exception提示吧,还会指出错误的具体位置的,根据那个去查吧~

你的代码中似乎忘记了调用InitialProgress()...

读书人网 >Android

热点推荐