Prashant Gaurav
Prashant Gaurav

Reputation: 215

How to include Progress Bar Before Going To Next Activity?

Am working on an android app in which there are list of activites.while launching any activity its talking sometime.So m thinking to include the progess bar till the next activity launched.

I have gone through a lot of question on progress bar but m not getting the exact solution.So,can any one suggest me the best way to integrate progress bar in my App.

I am Sharing My code and the logCat its not working.

  package com.example.progress;

  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;

  public class MainActivity extends Activity {

  private ProgressDialog progress;
  Button Button;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
     progress = new ProgressDialog(MainActivity.this);
     Button=(Button)findViewById(R.id.button1);

      Button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             switch (v.getId()) {
                case R.id.button1:
                     progress.setMessage("Please Wait Loading...");
               progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
               progress.setIndeterminate(true);
            progress.show();
             new Thread() 
             {

                 public void run() 
                 {

                     Intent i = new Intent(MainActivity.this,Second.class);
                     startActivity(i);
                     progress.dismiss();
                 }

             }.start();


                    break;
        }

        }
    });
    }
}

  LOgCat Error :::
  -----------   



       01-27 17:23:19.094: D/AndroidRuntime(13666): Shutting down VM
       01-27 17:23:19.095: E/AndroidRuntime(13666): FATAL EXCEPTION: main
       01-27 17:23:19.095: E/AndroidRuntime(13666): Process: com.example.progress, PID:  13666
       01-27 17:23:19.095: E/AndroidRuntime(13666): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.progress/com.example.progress.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        01-27 17:23:19.095: E/AndroidRuntime(13666):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
        01-27 17:23:19.095: E/AndroidRuntime(13666):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
      01-27 17:23:19.095: E/AndroidRuntime(13666):  at android.app.ActivityThread.access$800(ActivityThread.java:155)
      01-27 17:23:19.095: E/AndroidRuntime(13666):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
      01-27 17:23:19.095: E/AndroidRuntime(13666):  at android.os.Handler.dispatchMessage(Handler.java:102)
      01-27 17:23:19.095: E/AndroidRuntime(13666):  at android.os.Looper.loop(Looper.java:135)
     01-27 17:23:19.095: E/AndroidRuntime(13666):   at android.app.ActivityThread.main(ActivityThread.java:5343)
     01-27 17:23:19.095: E/AndroidRuntime(13666):   at java.lang.reflect.Method.invoke(Native Method)
      01-27 17:23:19.095: E/AndroidRuntime(13666):  at java.lang.reflect.Method.invoke(Method.java:372)
      01-27 17:23:19.095: E/AndroidRuntime(13666):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
      01-27 17:23:19.095: E/AndroidRuntime(13666):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:702)
      01-27 17:23:19.095: E/AndroidRuntime(13666): Caused by:   java.lang.NullPointerException: Attempt to invoke virtual method 'void  android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
       01-27 17:23:19.095: E/AndroidRuntime(13666):     at com.example.progress.MainActivity.onCreate(MainActivity.java:22)
       01-27 17:23:19.095: E/AndroidRuntime(13666):     at android.app.Activity.performCreate(Activity.java:6010)
       01-27 17:23:19.095: E/AndroidRuntime(13666):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
      01-27 17:23:19.095: E/AndroidRuntime(13666):  at      android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
       01-27 17:23:19.095: E/AndroidRuntime(13666):     ... 10 more

Upvotes: 0

Views: 2890

Answers (3)

ThaiPD
ThaiPD

Reputation: 3751

try to edit your onClick() as below to see the progressBar working Because your progressBar is belong to current Activity so when you start another Activity you are not able to see the progress bar

Button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             switch (v.getId()) {
                case R.id.button1:
                     progress.setMessage("Please Wait Loading...");
               progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
               progress.setIndeterminate(true);
            progress.show();
             new Thread() 
             {

                 public void run() 
                 {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
                     Intent i = new Intent(MainActivity.this,Second.class);
                     startActivity(i);
                     progress.dismiss();
                 }

             }.start();


                    break;
        }

        }
    });

Upvotes: 0

instanceof
instanceof

Reputation: 1466

How about starting the Progressbar when you click on the Button to go to the next activity or however you launch it, and then stop/close the bar in onPause()?

Upvotes: 1

Muhammad Younas
Muhammad Younas

Reputation: 1603

Let suppose your activity display simple list view

 private ProgressBar mProgBar;
mProgBar= (ProgressBar) findViewById(R.id.progressBarMain);
mProgBar.setVisibility(View.VISIBLE); //before doing your desired operation

/*     do your stuff here  populate your listview     */


mProgBar.setVisibility(View.INVISIBLE); //hide the progressbar
//display your list here

Upvotes: 0

Related Questions