开发者

using progress bar

I was trying to use a progress bar in my application - on a click of a button, a new layout with a progress bar widget on it should be displayed - i tried following code

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){

                 case R.id.Button:
            Intent intent = new Intent(Welcome.this, progbar.class);         
            startActivity(intent);
            break;
        }


public class progbar extends Activity{

    priva开发者_运维知识库te ProgressBar prgbar;
    private int prgStatus = 0;
    private Handler mHandler = new Handler();

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

        prgbar = (ProgressBar) findViewById(R.id.ReceiveUAI_prg);

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

                    // Update the progress bar
                    mHandler.post(new Runnable() {
                        public void run() {
                            prgbar.setProgress(prgStatus);
                        }
                    });
                }
            }
        }).start();
    }
}

but my application gets terminated on clicking the button.


put a progressbar in R.layout.lProgbar at design time. when you are done with it call progressbar.GONE, which will remove the progressbar.


Avoid the thread , Also you are not giving a delay in updating your progress bar status..so it ends up quickly..try this

 while (prgStatus < 100) {
                prgStatus += 2;

                // Update the progress bar
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        prgbar.setProgress(prgStatus);
                    }
                },200);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜