[英]Issues In Splash Screen with progressDialog
我的問題是進度對話框不顯示啟動畫面?任何人都可以解決這個問題,任何幫助都可以提前感謝!
public class Splash extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
Thread thread = new Thread(){
@Override
public void run() {
try
{
sleep(3*1000);
ProgressDialog progressDialog = new ProgressDialog(Splash.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("wait");
progressDialog.setCancelable(false);
progressDialog.show();
}catch (Exception e)
{
e.printStackTrace();
}finally {
Intent i = new Intent(Splash.this,MainActivity.class);
startActivity(i);
finish();
}
}
};thread.start();
}
}
public class Splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
ProgressDialog progressDialog = new ProgressDialog(Splash.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("wait");
progressDialog.setCancelable(false);
progressDialog.show();
Thread thread = new Thread(){
@Override
public void run() {
try
{
sleep(3*1000);
}catch (Exception e)
{
e.printStackTrace();
}finally {
progressDialog.dismiss();
Intent i = new Intent(Splash.this,MainActivity.class);
startActivity(i);
finish();
}
}
};thread.start();
}
}
因為您正在后台線程中更新 UI.. 嘗試使用
runOnUiThread(new Runnable.......)
或者嘗試將 UI 工作放在 UI 線程上。
您在線程中編寫的所有內容都將在后台執行。 您不能從后台 Thread 操作任何 UI 元素。 你應該從這段代碼中得到一個錯誤,檢查你的堆棧跟蹤。 我建議您從線程中刪除 ProgressDialog 的代碼並將其放在線程之前。
您應該從 UI 線程顯示進度對話框。 或者您可以使用 runOnUiThread(...) 方法。 如果您必須從不同的線程顯示它,請在線程的 run 方法中寫入 Thin:
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
//add try catch
sleep(3*1000);
ProgressDialog progressDialog = new ProgressDialog(Splash.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
});
我建議在您的活動中使用處理程序而不是睡眠。 您也可以在代碼中沒有線程的情況下嘗試此操作:
Handler h = new Handler(Looper.getMainLooper())
h.postDelayed( new Runnable() {
@Override
public void run() {
ProgressDialog progressDialog = new ProgressDialog(Splash.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
},
(3*1000));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.