android 闪屏中的进度条加载错误
Progress Bar loading error in android splashscreen
希望每个人的健康和精神都很好,我是新手,或者你可以说 starter/beginner 水平 android 发展,目前我面临一个关于闪屏的问题。我面临的问题是我在我的主 activity 中使用了一个进度条,它应该以 20 的间隔加载进度条,但它不会加载并在不加载进度条的情况下继续等待,在模拟器上运行之前,一切正常,没有错误。一旦项目在模拟器上启动,进度条就不会加载(我的意思是安装后)并继续等待并且不打算使用 SecondActivity。我使用了睡眠时间为 3000 的线程,如果我没记错的话,这可能是唯一的错误原因,因为其余代码对我来说似乎没问题。我正在粘贴下面的代码,请看一下我被深深地困住了。等待你的解决方案的朋友!
public class 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);
}
}
我想你的意思是 progress += 20
,而不是 progress = +20
。可能您的 progress
卡在了 20,因此从不调用 startActivity。
您的问题是您尝试通过调用 pg.setProgress(progress);
在 UI Thread
之外更新 ProgressBar
。您可以像这样修复它:
ProgressBar pg;
int progress=0;
Handler h = new Handler(new Callback(){
public boolean hanldeMessage(Message msg){
if(msg.what == 1)
pg.setProgress(progress);
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pg = (ProgressBar)findViewById(R.id.progressBar1);
// TODO Auto-generated method stub
h.post(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
progress += 20;
h.sendEmptyMessage(1);
// TODO Auto-generated method stub
if (progress == pg.getMax()) {
Intent in = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(in);
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO: handle exception
}
});
}
}
您可以在 here
阅读更多关于与 UI Thread
交流的信息
希望每个人的健康和精神都很好,我是新手,或者你可以说 starter/beginner 水平 android 发展,目前我面临一个关于闪屏的问题。我面临的问题是我在我的主 activity 中使用了一个进度条,它应该以 20 的间隔加载进度条,但它不会加载并在不加载进度条的情况下继续等待,在模拟器上运行之前,一切正常,没有错误。一旦项目在模拟器上启动,进度条就不会加载(我的意思是安装后)并继续等待并且不打算使用 SecondActivity。我使用了睡眠时间为 3000 的线程,如果我没记错的话,这可能是唯一的错误原因,因为其余代码对我来说似乎没问题。我正在粘贴下面的代码,请看一下我被深深地困住了。等待你的解决方案的朋友!
public class 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);
}
}
我想你的意思是 progress += 20
,而不是 progress = +20
。可能您的 progress
卡在了 20,因此从不调用 startActivity。
您的问题是您尝试通过调用 pg.setProgress(progress);
在 UI Thread
之外更新 ProgressBar
。您可以像这样修复它:
ProgressBar pg;
int progress=0;
Handler h = new Handler(new Callback(){
public boolean hanldeMessage(Message msg){
if(msg.what == 1)
pg.setProgress(progress);
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pg = (ProgressBar)findViewById(R.id.progressBar1);
// TODO Auto-generated method stub
h.post(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
progress += 20;
h.sendEmptyMessage(1);
// TODO Auto-generated method stub
if (progress == pg.getMax()) {
Intent in = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(in);
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO: handle exception
}
});
}
}
您可以在 here
阅读更多关于与UI Thread
交流的信息