简体   繁体   中英

Progress Bar loading error in android splashscreen

hope everyone's doing fine with their health and spirit, i am new or you can say at starter/beginner levels of android development and currently I'm facing an issue regarding splash screen. The trouble I'm facing is that I've used a progress bar in my main activity which is suppose to load the progress bar at intervals of 20 but it doesn't load and keeps on waiting without loading the progress bar, everything goes on fine without errors until it runs on emulator. Once the project gets launched on emulator the progress bar does not load (i mean after installation) and keeps on waiting and waiting and doesn't intent to the SecondActivity. I have used thread with a sleep time of 3000, if I'm not wrong it might be the only cause of error because the rest of code seems fine to me. I'm pasting the code below, please do have a look at it I'm badly stuck into it. Waiting for your solution friends!

public class MainActivity extends 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);
}

}

I think you meant progress += 20 , not progress = +20 . Probably your progress is being stuck to 20, so startActivity is never called.

Your problem is you try to update ProgressBar outside UI Thread by calling pg.setProgress(progress); . You can fix it like:

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
            }
        });

    }

}

You can read more about communicating with UI Thread at here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM