我的问题是进度对话框没有显示启动画面?可以解决这个问题,任何帮助都可以提前感谢!
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();
}
}
答案 0 :(得分:1)
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();
}
}
答案 1 :(得分:0)
因为你正在后台线程中更新UI .. 尝试使用
runOnUiThread(new Runnable.......)
或尝试将UI工作放在UI线程上。
答案 2 :(得分:0)
你在线程内写的内容将在后台执行。你无法操纵后台线程中的任何UI元素。你应该从这段代码中得到一个错误,检查你的堆栈跟踪。我建议你从Thread中删除ProgressDialog的代码并将它放在Thread之前。
答案 3 :(得分:0)
您应该从UI线程显示进度对话框。或者您可以使用runOnUiThread(...)方法。如果你必须从不同的线程中显示它,请在线程的内部运行方法中写入:
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));