无法在android中使用Asynctask显示动画对话框



我正在尝试使用AsyncTask在android中显示自定义动画对话框,但我现在在log-cat中遇到了一些问题,如下所示。谁能告诉我怎么解决这个问题?

LOGCAT

06-20 09:42:47.981: E/AndroidRuntime(668): FATAL EXCEPTION: main
06-20 09:42:47.981: E/AndroidRuntime(668): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.customdialog_sample/com.example.customdialog_sample.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.os.Looper.loop(Looper.java:123)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-20 09:42:47.981: E/AndroidRuntime(668):  at java.lang.reflect.Method.invokeNative(Native Method)
06-20 09:42:47.981: E/AndroidRuntime(668):  at java.lang.reflect.Method.invoke(Method.java:507)
06-20 09:42:47.981: E/AndroidRuntime(668):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-20 09:42:47.981: E/AndroidRuntime(668):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-20 09:42:47.981: E/AndroidRuntime(668):  at dalvik.system.NativeStart.main(Native Method)
06-20 09:42:47.981: E/AndroidRuntime(668): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.view.ViewRoot.setView(ViewRoot.java:531)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.app.Dialog.show(Dialog.java:241)
06-20 09:42:47.981: E/AndroidRuntime(668):  at com.example.customdialog_sample.LoadingDialog.LodingDialog1(LoadingDialog.java:60)
06-20 09:42:47.981: E/AndroidRuntime(668):  at com.example.customdialog_sample.LoadingDialog.<init>(LoadingDialog.java:19)
06-20 09:42:47.981: E/AndroidRuntime(668):  at com.example.customdialog_sample.MainActivity$CustomLoad.<init>(MainActivity.java:66)
06-20 09:42:47.981: E/AndroidRuntime(668):  at com.example.customdialog_sample.MainActivity.onCreate(MainActivity.java:58)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-20 09:42:47.981: E/AndroidRuntime(668):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-20 09:42:47.981: E/AndroidRuntime(668):  ... 11 more

MainActiviy.java

public class MainActivity extends Activity
{
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.dialog);

          CustomLoad task = new CustomLoad();
          task.execute();
   }
   public class CustomLoad extends AsyncTask<Void, Void, Void>
   {
       LoadingDialog load_instance = new LoadingDialog(getApplicationContext());
       @Override
       protected void onPreExecute() {
        // TODO Auto-generated method stub
        // super.onPreExecute();
         load_instance.LodingDialog1(MainActivity.this, "Rounding...", false);

       }
       @Override
       protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        return null;
       }
       @Override
       protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        // super.onPostExecute(result);
           load_instance.hide();
       }}}

LoadingDialog.java

public class LoadingDialog extends Dialog
{
   private Dialog mpd = null;
   private LayoutInflater lyt_Inflater = null;
   public LoadingDialog(Context context)
   {     
          super(context);
          LodingDialog1(context,"Loading...",false);
   }

   public LoadingDialog(Context context,String LoadingText)
   {
          super(context);
          LodingDialog1(context,LoadingText,false);
   }
   public LoadingDialog(Context context,String LoadingText, boolean cancelable)
   {
          super(context);
          LodingDialog1(context,LoadingText,cancelable);
   }
   public void LodingDialog1(Context context,String LoadingText, boolean cancelable)
   {
          lyt_Inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          final View view_lyt = lyt_Inflater.inflate(     R.layout.dialog, null);
          ImageView imageView = (ImageView) view_lyt.findViewById(R.id.imageView1);
          Animation a = AnimationUtils.loadAnimation(context, R.anim.progress_anim);
          a.setDuration(2000);
          imageView.startAnimation(a);
          a.setInterpolator(new Interpolator()
          {
                 private final int frameCount = 8;
                 @Override
                 public float getInterpolation(float input)
                 {
                       return (float)Math.floor(input*frameCount)/frameCount;
                 }
          });
          mpd = new Dialog(context, R.style.ThemeDialogCustom);
          mpd.setContentView(view_lyt);
          mpd.setCancelable(cancelable);
          mpd.show();
   }
   public void hide()
   {
          if (mpd != null) {
                 if (mpd.isShowing())
                 {
                       mpd.dismiss();
                 }
          }}}

试试这个

   LoadingDialog load_instance = new LoadingDialog(MainActivity.this);
不是

   LoadingDialog load_instance = new LoadingDialog(getApplicationContext());

您应该将Activity上下文传递给LoadingDialog(MainActivity.this)

最新更新