2016-03-02 61 views
-9

我想處理一些數據庫操作,然後想開始一個新的活動。同時,我想要顯示進度條。Android應用程序崩潰,從不同的線程開始下一個活動

下面我寫的代碼在android studio以及某些設備上工作正常,但在某些設備上,從線程開始新活動時,應用程序正在崩潰。請幫幫我。

在此先感謝。

progress=new ProgressDialog(this); 
    progress.setMessage("Prepairing relations..."); 
    progress.setProgress(0); 
    progress.setCancelable(false); 
    progress.show(); 

    final Thread t1 = new Thread() { 
     @Override 
     public void run() { 
      sql.updateRelations(); // db operations 

      Intent intent = new Intent(context, MainActivity.class); 
      progress.dismiss(); 
      startActivity(intent); 
      finish(); 
     } 
    }; 
    t1.start(); 
+7

哪裏是你的logcat? –

回答

1

使用處理器從主線程啓動新的活動

Handler handler = new Handler(Looper.getMainLooper()); 
1

試試這個代碼: -

progress=new ProgressDialog(this); 
progress.setMessage("Prepairing relations..."); 
progress.setProgress(0); 
progress.setCancelable(false); 
progress.show(); 

Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     sql.updateRelations(); // db operations 
     Intent intent = new Intent(NameActivity.this, MainActivity.class); 
     progress.dismiss(); 
     startActivity(intent); 
     finish(); 
    } 
}, 15000);