安卓 splashscreen中的java进度条加载错误
希望每个人在健康和精神方面都做得很好,我是新来的,或者你可以说在安卓开发的初级/初级阶段,目前我面临一个关于启动屏幕的问题。我面临的问题是,我在我的主要活动中使用了一个进度条,它假设每隔20次加载进度条,但它不加载进度条,并在不加载进度条的情况下继续等待,直到它在emulator上运行为止,一切都很好,没有错误。一旦项目在emulator上启动,进度条就不会加载(我的意思是在安装之后),并且一直在等待,并且不打算执行第二个活动。我使用了睡眠时间为3000的线程,如果我没有错的话,它可能是导致错误的唯一原因,因为其余代码对我来说似乎很好。我正在粘贴下面的代码,请看一看,我被卡住了。等待你的解决方案,朋友们
公共类MainActivity扩展了AppCompatActivity{
ProgressBar pg;
int progress=0;
Handler h = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pg = (ProgressBar)findViewById(R.id.progressBar1);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
progress = +20;
h.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
pg.setProgress(progress);
if (progress == pg.getMax()) {
Intent in = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(in);
}
}
});
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}
}).start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
# 1 楼答案
你的问题是你试图通过调用
pg.setProgress(progress);
来更新ProgressBar
外部UI Thread
。你可以像这样修复它:你可以在here阅读更多关于与
UI Thread
交流的信息# 2 楼答案
我想你指的是
progress += 20
,不是progress = +20
。可能你的progress
被固定在20,所以永远不会调用startActivity