2012-05-16 106 views
0
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     loadData(); 
     setContentView(R.layout.preload); 

     Thread thread = new Thread() 
     { 
      @Override 
      public void run() { 
       setUpDB(); 
       setContentView(R.layout.main); 
      } 
     }; 
     thread.start(); 
    } 

所以我希望它顯示數據庫設置時的預加載視圖(setUpDB),然後在安裝完成後切換到主視圖切換到主視圖。我有它幾乎工作;顯示加載器直到大約80%,然後崩潰,但現在我甚至無法讓它顯示加載器...如果我有setUpDB()在線程崩潰,如果我有線程之外它顯示一個空白屏幕直到完全加載。我不記得我以前的代碼......有什麼想法?如何在創建數據庫時顯示加載屏幕?

+0

什麼你在做setupDb嗎? – Tarun

回答

2

聲明一個處理程序,並調用它來更新你的UI這樣,

public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

ProgressDialog pd=ProgressDialog.show(this,"","Please Wait..."); 
Handler handler =new Handler() 
{ 
public void handleMessage(Message msg) 
{ 
if(msg.what==0) 
{ 
pd.dismiss(); 
setContentView(R.layout.main); 
} 
} 
}; 
      Thread thread = new Thread() 
      { 
       @Override 
       public void run() { 
        loadData(); 
        setUpDB(); 
        handler.sendEmptyMEssage(0); 
       } 
      }; 
      thread.start(); 
     } 
+0

hmm ...在數據庫完全加載之前進入主視圖 – Chimeara

+0

然後,您可以顯示啓動畫面或其他內容,而不是進度對話框。 –

+0

其實這在我的設備上正常工作,但在模擬器上崩潰。謝謝 – Chimeara

0
setUpDB(); 
    splashHandler.sendEmptyMessageDelayed(STOP_SPLASH, STOP_SPLASH_DELAY); 

private Handler splashHandler = new Handler() { 
    public void handleMessage(android.os.Message msg) { 

     switch (msg.what) { 
     case STOP_SPLASH: 
          progressbar.dismiss(); 
      startActivity(new Intent(getApplicationContext(), 
        HomeScreenActivity.class)); 
      finish(); 
      break; 
     } 
    }; 
}; 
4

試試這個使用assynctask ..

  private class LoadAssync extends AsyncTask<String, Void, Void> { // Assync task 

    protected void onPreExecute() { 

      ProgressDialog dialog=ProgressDialog.show(this,"","Loading"); 

    } 

    protected Void doInBackground(final String... args) { 
     setUpDB(); 

    } 

    protected void onPostExecute(final Void unused) { 


     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 

    } 
} 

執行assynctask

LoadAssync mAsyync1; 

mAsyync1 = new LoadAssync(); 
      mAsyync1.execute(null); 
+0

但是你如何在AsyncTask中設置ContentView? – Chimeara

+0

在post執行中設置contentview –

相關問題