0

我有 AsyncTask 扩展类我想显示进度对话框,所以我使用以下代码

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

调用它

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



}

但它总是崩溃,我不知道如何初始化进度并将上下文添加到 i1t,给这个日志

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)
4

2 回答 2

1

您不要从您的活动课程中发送任何参考。您发送以传递上下文。

这是解决方案。请检查一下。

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

}

谢谢

于 2012-06-10T12:20:53.403 回答
1

您必须将 Context 对象传递给 ProgressTask。

改变这个

connectionProgressDialog = new ProgressDialog(null);

connectionProgressDialog = new ProgressDialog(context);

现在下一个问题是我将如何获得上下文。

更改您的代码以包含上下文参数构造函数。

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

更改您的呼叫代码

new EncounterBackgroundWorker().execute();

new EncounterBackgroundWorker(getActivity()).execute();
于 2012-06-10T12:31:09.783 回答