AMH
AMH

Reputation: 6451

progress bar on AsyncTaskcrash

I have AsyncTask extend class I want to display progress dialoge so I use the following code

 class EncounterBackgroundWorker extends AsyncTask<Void, Void, Void> {

      ProgressDialog connectionProgressDialog;
      @Override
      protected void onPreExecute() {


          connectionProgressDialog = new ProgressDialog(null);
          connectionProgressDialog.setCancelable(false);
          connectionProgressDialog.setCanceledOnTouchOutside(false);
          connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
          connectionProgressDialog.setMessage("Uploading Leads...");
          connectionProgressDialog.show();
      }
}

call it in

public class ChemoUnit  extends Fragment {


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {


         View view = inflater.inflate(
                    R.layout.chemounit,
                    container,
                    false);


        //Intialize the record Grid
         LinearLayout formLayout = (LinearLayout)view.findViewById(R.id.ChemoUnitGrid);
        formLayout.removeAllViews();

        MainGrid = new ListView(getActivity().getApplicationContext());              
        MainGrid.setVisibility(ListView.VISIBLE);
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
         params.gravity = Gravity.RIGHT;
         MainGrid.setLayoutParams(params);



         //1. set the header 

         ViewGroup header = (ViewGroup)inflater.inflate(R.layout.chemounitgridheader, MainGrid, false);
         MainGrid.addHeaderView(header, null, false);

         //2. Call the web Service 


         new EncounterBackgroundWorker( getActivity()).execute();

         MainGrid.setAdapter(new Encounteradapter(view.getContext(), null));
        // Finally add it 

         formLayout.addView(MainGrid);



         // Return the Result 
        return inflater.inflate(R.layout.chemounit, container, false);
    }



}

but it always crash , I don't know how initialize the progress and add context to i1t , give this log

06-10 14:49:48.665: D/dalvikvm(10728): GC_CONCURRENT freed 143K, 4% free 6768K/6983K, paused 3ms+4ms
06-10 14:49:51.465: D/dalvikvm(10728): GC_FOR_ALLOC freed 46K, 4% free 6824K/7047K, paused 39ms
06-10 14:49:51.465: I/dalvikvm-heap(10728): Grow heap (frag case) to 7.223MB for 513744-byte allocation
06-10 14:49:51.505: D/dalvikvm(10728): GC_FOR_ALLOC freed <1K, 4% free 7326K/7559K, paused 25ms
06-10 14:49:51.535: I/System.out(10728): Chemo Unit
06-10 14:49:51.540: I/System.out(10728): Next Appointement
06-10 14:49:51.545: I/System.out(10728): Change Booking
06-10 14:49:51.545: I/System.out(10728): Booking
06-10 14:49:51.550: D/dalvikvm(10728): GC_CONCURRENT freed 3K, 3% free 7353K/7559K, paused 2ms+4ms
06-10 14:49:51.555: I/System.out(10728): Chemo Unit
06-10 14:49:51.555: I/System.out(10728): Next Appointement
06-10 14:49:51.560: I/System.out(10728): Change Booking
06-10 14:49:51.565: I/System.out(10728): Booking
06-10 14:49:51.565: I/System.out(10728): Chemo Unit
06-10 14:49:51.570: I/System.out(10728): Next Appointement
06-10 14:49:51.570: I/System.out(10728): Change Booking
06-10 14:49:51.570: I/System.out(10728): Booking
06-10 14:49:51.575: I/System.out(10728): Chemo Unit
06-10 14:49:51.575: I/System.out(10728): Next Appointement
06-10 14:49:51.585: I/System.out(10728): Change Booking
06-10 14:49:51.590: I/System.out(10728): Booking
06-10 14:49:51.590: I/System.out(10728): Chemo Unit
06-10 14:49:51.595: I/System.out(10728): Next Appointement
06-10 14:49:51.595: I/System.out(10728): Change Booking
06-10 14:49:51.600: I/System.out(10728): Booking
06-10 14:49:55.410: D/CLIPBOARD(10728): Hide Clipboard dialog at Starting input: finished by someone else... !
06-10 14:50:00.075: I/System.out(10728): Chemo Unit
06-10 14:50:00.080: I/System.out(10728): Next Appointement
06-10 14:50:00.085: I/System.out(10728): Change Booking
06-10 14:50:00.090: I/System.out(10728): Booking
06-10 14:50:00.090: I/System.out(10728): Chemo Unit
06-10 14:50:00.090: I/System.out(10728): Next Appointement
06-10 14:50:00.095: I/System.out(10728): Change Booking
06-10 14:50:00.095: I/System.out(10728): Booking
06-10 14:50:00.100: I/System.out(10728): Chemo Unit
06-10 14:50:00.100: I/System.out(10728): Next Appointement
06-10 14:50:00.105: I/System.out(10728): Change Booking
06-10 14:50:00.105: I/System.out(10728): Booking
06-10 14:50:00.105: I/System.out(10728): Chemo Unit
06-10 14:50:00.105: I/System.out(10728): Next Appointement
06-10 14:50:00.105: I/System.out(10728): Change Booking
06-10 14:50:00.110: I/System.out(10728): Booking
06-10 14:50:01.995: I/System.out(10728): Chemo Unit
06-10 14:50:01.995: I/System.out(10728): Next Appointement
06-10 14:50:01.995: I/System.out(10728): Change Booking
06-10 14:50:02.005: I/System.out(10728): Booking
06-10 14:50:02.010: I/System.out(10728): Chemo Unit
06-10 14:50:02.010: I/System.out(10728): Next Appointement
06-10 14:50:02.010: I/System.out(10728): Change Booking
06-10 14:50:02.010: I/System.out(10728): Booking
06-10 14:50:02.015: I/System.out(10728): Chemo Unit
06-10 14:50:02.015: I/System.out(10728): Next Appointement
06-10 14:50:02.015: I/System.out(10728): Change Booking
06-10 14:50:02.015: I/System.out(10728): Booking
06-10 14:50:02.020: I/System.out(10728): Chemo Unit
06-10 14:50:02.020: I/System.out(10728): Next Appointement
06-10 14:50:02.020: I/System.out(10728): Change Booking
06-10 14:50:02.020: I/System.out(10728): Booking
06-10 14:50:02.050: D/AndroidRuntime(10728): Shutting down VM
06-10 14:50:02.050: W/dalvikvm(10728): threadid=1: thread exiting with uncaught exception (group=0x401b4760)
06-10 14:50:02.050: E/AndroidRuntime(10728): FATAL EXCEPTION: main
06-10 14:50:02.050: E/AndroidRuntime(10728): java.lang.NullPointerException
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:120)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.app.AlertDialog.<init>(AlertDialog.java:80)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.app.ProgressDialog.<init>(ProgressDialog.java:76)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at com.appnetics.EncounterBackgroundWorker.onPreExecute(EncounterBackgroundWorker.java:45)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:549)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.os.AsyncTask.execute(AsyncTask.java:499)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at com.appnetics.ChemoUnit.onCreateView(ChemoUnit.java:63)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.os.Handler.handleCallback(Handler.java:587)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.os.Looper.loop(Looper.java:132)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at android.app.ActivityThread.main(ActivityThread.java:4123)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at java.lang.reflect.Method.invokeNative(Native Method)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at java.lang.reflect.Method.invoke(Method.java:491)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-10 14:50:02.050: E/AndroidRuntime(10728):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 463

Answers (2)

Vipul
Vipul

Reputation: 28093

You must pass Context object to ProgressTask.

Change this

connectionProgressDialog = new ProgressDialog(null);

to

connectionProgressDialog = new ProgressDialog(context);

Now next questions comes is how will i get the context.

Change your code to include context argument constructer.

class EncounterBackgroundWorker extends AsyncTask<Void, Void, Void> {

      private ProgressDialog connectionProgressDialog;
      private Context context;

      public EncounterBackgroundWorker(Context context){
         this.context=context;
      }

      @Override
      protected void onPreExecute() {


          connectionProgressDialog = new ProgressDialog(context);
          connectionProgressDialog.setCancelable(false);
          connectionProgressDialog.setCanceledOnTouchOutside(false);
          connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
          connectionProgressDialog.setMessage("Uploading Leads...");
          connectionProgressDialog.show();
      }
}

Change you calling code

From

new EncounterBackgroundWorker().execute();

To

new EncounterBackgroundWorker(getActivity()).execute();

Upvotes: 1

Md Abdul Gafur
Md Abdul Gafur

Reputation: 6201

you Do not send any Reference From your activity calss. you send to pass Context.

Here is Solution. please Check it.

     class  EncounterBackgroundWorker extends AsyncTask<Void, Void, Void> {
     private Context context;
   public EncounterBackgroundWorker(Context Context)
   {
     this.context=context;
    }

  ProgressDialog connectionProgressDialog;
  @Override
  protected void onPreExecute() {


      connectionProgressDialog = new ProgressDialog(context);
      connectionProgressDialog.setCancelable(false);
      connectionProgressDialog.setCanceledOnTouchOutside(false);
      connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
      connectionProgressDialog.setMessage("Uploading Leads...");
      connectionProgressDialog.show();
  }

}

Thanks

Upvotes: 1

Related Questions