2012-02-10 7 views
0

저는 ProgressBar 클래스를 Android에서 사용하고 있지만 5 초 동안 진행할 수 없으며 응용 프로그램을로드 할 수 없습니다. 모든 것은 작동하지만 진행 표시 줄이 진행되지 않습니다. 여기에 코드가 있습니다. 여기 스레드가있는 Android 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(); 
} 

} 

그리고

당신은 다른 스레드에서 UI 위젯을 업데이트 할 수 없습니다 레이아웃 파일 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

입니다. 당신은 다음과 같이해야합니다 :

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

관련 문제