2012-02-10 12 views
0

私はProgressBarクラスをAndroidで使用していますが、5秒間進まずにアプリケーションをロードすることはできません。すべてが機能しますが、進行状況バーは進行しません。ここにコードがあります。スレッド付きのProgressBar

public class StartPoint extends Activity{ 

ProgressBar progressBar; 
private int progressBarStatus = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 

    progressBar = (ProgressBar)findViewById(R.id.progressBar1); 


    Thread timer = new Thread(){ 
     public void run(){ 
      try{ 
       sleep(5000); 
       while(progressBarStatus < 5000){ 
        progressBar.setProgress(progressBarStatus); 
        progressBarStatus += 1000; 

       } 
      }catch(InterruptedException e){ 
       e.printStackTrace(); 
      }finally{ 
       Intent openMainList = new Intent(StartPoint.this, in.isuru.caf.MainList.class); 
       startActivity(openMainList); 
      } 
     } 
    }; 
    timer.start(); 
} 

protected void onPause(){ 
    super.onPause(); 
    finish(); 
} 

} 

そして、ここでレイアウトファイルsplash.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/mary_mother_of_god" /> 

<ProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.67" /> 

</LinearLayout> 

答えて

9

であるあなたは、異なるスレッドからUIウィジェットを更新することはできません。あなたは次のようなことをする必要があります:

Thread timer = new Thread(){ 
    public void run(){ 
     try{ 
      sleep(5000); 
      while(progressBarStatus < 5000){ 
       StartPoint.this.runOnUIThread(new Runnable(){ 
        public void run() 
        { 
         progressBar.setProgress(progressBarStatus); 
         progressBarStatus += 1000; 
        } 
       }); 

      } 
     }catch(InterruptedException e){ 
      e.printStackTrace(); 
     }finally{ 
      Intent openMainList = new Intent(StartPoint.this, in.isuru.caf.MainList.class); 
      startActivity(openMainList); 
     } 
    } 
}; 
timer.start(); 
+0

私はそれを得ません。もっと具体的にするか、コード全体を投稿することができますか?上記の方法から始めて5000ミリ秒寝るか、別に宣言する必要がありますか? – Isuru

+0

上記のコードを編集しました。 – CaseyB

+0

ありがとうございました! – Isuru