Android启动画面中的进度条加载错误

时间:2015-08-29 15:11:13

标签: java android multithreading splash-screen

希望每个人的健康和精神都很好,我是新手,或者你可以说在Android开发的初级/初级水平,目前我正面临着关于启动画面的问题。我遇到的麻烦是我在我的主要活动中使用了一个进度条,假设以20为间隔加载进度条但它没有加载并继续等待而不加载进度条,一切都在继续在模拟器上运行之前没有错误。一旦项目在模拟器上启动,进度条就不会加载(我的意思是在安装之后)并继续等待和等待而不是意图使用SecondActivity。我已经使用了休眠时间为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);
}

}

2 个答案:

答案 0 :(得分:0)

我认为你的意思是progress += 20,而不是progress = +20。可能你的progress被卡住了20,所以永远不会调用startActivity。

答案 1 :(得分:0)

您的问题是,您是通过致电ProgressBar尝试更新UI Thread之外的pg.setProgress(progress);。您可以修复它:

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沟通的更多信息