2012-05-08 25 views
0

加載進度欄後無法啓動新活動。我在進度欄上陷入困境,不知道應該在哪裏放置新的活動。任何解決我的問題?已加載進度欄後的新活動 - Android -

ProgressBar myProgressBar; 
int myProgress = 0; 

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

    myProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal); 

    new Thread(myThread).start(); 
} 

private Runnable myThread = new Runnable(){ 

    public void run() { 
     // TODO Auto-generated method stub 
     while (myProgress<100){ 
      try{ 
       myHandle.sendMessage(myHandle.obtainMessage()); 
       Thread.sleep(50); 
      } 
      catch(Throwable t){ 

      } 
     } 
    } 
    Handler myHandle = new Handler(){ 

     @Override 
     public void handleMessage(Message msg) { 
      // TODO Auto-generated method stub 
      myProgress++; 
      myProgressBar.setProgress(myProgress); 
     } 
    }; 
}; 

}

+0

而不是通用線程,您可能想要使用AsyncTask。你可以使用'publishProgress()'和'onProgressUpdate()'來更新你的進度條,然後在'onPostExecute()'中創建你的Intent [http://developer.android.com/reference/android/os/AsyncTask.html – MattDavis

回答

0
public void run() { 
    //don't hard code things, use a constant for max progress value 
    while (myProgress<MAX_PROGRESS){ 
     try{ 
      myHandle.sendMessage(myHandle.obtainMessage()); 
      //same 
      Thread.sleep(SLEEP_TIME); 
     } catch(Exception ex){ 
      //never live an empty catch statement, you are missing exceptions and 
      //can't correct them 
      Log.e("MyCurrentClass", "Error during async data processing",e); 
     }//catch 
    }//while 
    //start new activity here 
    startActivity(MyClass.this, NewActivity.class); 
}//met 
2

請添加到您的按鈕單擊事件。

  progressDialog = ProgressDialog.show(this, "", "Loading..."); 
      new Thread() { 

       public void run() { 

        Intent ddIntent = new Intent(
          MainActivity.this, MainWall.class); 
        startActivity(ddIntent); 
        progressDialog.dismiss(); 
       } 

      }.start();