当我致电ProgressDialog
课程时,我的AsyncTask
崩溃了。
AsyncTask
会抛出以下错误:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
这就是我从AsyncTask
类
Activity
类的方法
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)
感谢您的帮助。
此致
答案 0 :(得分:1)
我运行了您的代码并得出结论,您使用的是Context
getApplicationContext()
而不是Activity
上下文。
请注意,这与传递给Context
的{{1}}相同。