2011-03-29 83 views
0

我试图用我的应用程序的进度条 - 在按钮的点击,应显示上有一个进度条部件新布局 - 我想下面的代码使用进度条

@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{ 

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

但是我的应用程序在点击按钮时被终止。

+1

什么是堆栈跟踪的细节? – Mudassir 2011-03-29 10:19:03

+0

堆栈跟踪意味着logcat或控制台? – bhabs 2011-03-29 10:41:45

+0

是的,logcat。它显示了异常的细节。 – Mudassir 2011-03-29 10:44:58

回答

0

在设计时在R.layout.lProgbar中放置了一个进度条。当你完成它时,调用progressbar.GONE,它将删除进度条。

+0

我已经在设计时添加了它......并且由于进度条并未出现(我的应用程序崩溃),因此无法关闭! – bhabs 2011-03-29 11:30:07

+0

发布完整的堆栈跟踪。 – Umesh 2011-03-29 11:58:47

0

避免线程,你也没有给在更新进度条延迟status..so它结束了quickly..try这

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

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

谢谢!但是这并没有帮助我理清我的问题.., – bhabs 2011-03-29 11:09:47