【问题标题】:progressdialog crash in AsyncTask classAsyncTask 类中的进度对话框崩溃
【发布时间】:2014-06-16 01:38:06
【问题描述】:

当我调用 AsyncTask 类时,我的 ProgressDialog 崩溃。 AsyncTask 抛出以下错误:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

这就是我从Activity 类中调用我的AsyncTask 类的方式

new CopyToSDHelper(Environment.getExternalStorageDirectory()
            + File.separator + fileName, context, fileName, numberOfContact).execute();

这是我的 AsyncTask 类

public class CopyToSDHelper extends AsyncTask<Object, Integer, Void>
{
    // Notification Manager
    private NotificationManager myNotificationManager;
    // Current API version
    private int                 currentApiVersion;

    String                      fileName        = "";
    String                      backUpFileName  = "";
    String                      numberOfContact = "";
    Context                     context;
    ProgressDialog              dialog;

    @Override
    protected void onPreExecute()
    {
        dialog = new ProgressDialog(context);
        dialog.setMessage("Uploading...");
        dialog.setIndeterminate(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgress(0);
        dialog.show();
        dialog.setContentView(R.layout.activity_background_submit_dialog);
    }

    public CopyToSDHelper(String fileName, Context context,
            String backUpFileName, String numberOfContact)
    {
        this.fileName = fileName;
        this.backUpFileName = backUpFileName;
        this.numberOfContact = numberOfContact;
        this.context = context;

        this.currentApiVersion = android.os.Build.VERSION.SDK_INT;
    }


    @Override
    protected Void doInBackground(Object... params)
    {
        // Call Notification method
        displayNotification();

        WebDavWriter writer = new WebDavWriter(context, fileName);
        try
        {
            int progress = Integer.parseInt(numberOfContact) / 100;

            publishProgress(progress);
            writer.sendFileToKiki(backUpFileName);
            Thread.sleep(2000);

            // Now delete local file from Android
            File file = new File(fileName);
            file.delete();
        }
        catch (Exception e)
        {
            new ErrLog(e.toString());
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress)
    {
        dialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(Void result)
    {
        try
        {
            dialog.dismiss();
        }
        catch (Exception e)
        {
        }
    }
}

这是 Logcat 结果

E/AndroidRuntime(29186): FATAL EXCEPTION: main
E/AndroidRuntime(29186): Process: example.sample.android.SDcontactlist, PID: 29186
E/AndroidRuntime(29186): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime(29186): at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
E/AndroidRuntime(29186): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
E/AndroidRuntime(29186): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
E/AndroidRuntime(29186): at android.app.Dialog.show(Dialog.java:286)
E/AndroidRuntime(29186): at write_helper.CopyToSDHelper.onPreExecute(CopyToSDHelper.java:43)
E/AndroidRuntime(29186): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
E/AndroidRuntime(29186): at android.os.AsyncTask.execute(AsyncTask.java:535)
E/AndroidRuntime(29186): at utils.SaveAllEntry.vcfSection(SaveAllEntry.java:281)
E/AndroidRuntime(29186): at utils.SaveAllEntry.access$10(SaveAllEntry.java:246)
E/AndroidRuntime(29186): at utils.SaveAllEntry$4.onClick(SaveAllEntry.java:201)
E/AndroidRuntime(29186): at android.view.View.performClick(View.java:4438)
E/AndroidRuntime(29186): at android.view.View$PerformClick.run(View.java:18422)
E/AndroidRuntime(29186): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(29186): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(29186): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(29186): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(29186): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(29186): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(29186): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(29186): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(29186): at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助。

问候

【问题讨论】:

  • 发布你的activity_background_submit_dialog

标签: android android-asynctask progressdialog


【解决方案1】:

我运行了您的代码并得出结论,您使用的是 Context,例如 getApplicationContext(),而不是 Activity 上下文。

请注意,这与传递给 Toast.makeText()Context 相同。

【讨论】:

  • 亲爱的苏菲安。谢谢你的答复。我已经按照您的建议进行了更改,不幸的是再次崩溃并抛出此消息:android.util.AndroidRuntimeException:在添加内容之前必须调用 requestFeature()
  • 您的回答给了我新的视野,我解决了问题。解决方案很简单。我使用 ClassName.this 而不是 getApplicationContext();希望这对其他人有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多