ProgressDialog在onPreExecute()方法中崩溃。
以下是我的代码段:
public class TestCasesActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testcases);
xmlHelp = new XmlHelper();
ConfigParser confParser = new ConfigParser();
confParser.execute();
}
private class ConfigParser extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(TestCasesActivity.this);
dialog.setMessage("Loading TestCases Configuration...");
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
xmlHelp.getNumberOfNodes();
return null;
}
@Override
protected void onPostExecute(Void v) {
dialog.dismiss();
// Show test cases list here via a ListView
}
}
请在此处说明可能出现的问题。 logcat打印如下:
07-30 08:13:50.511 20200-20200 /? E /窗口管理器: android.view.WindowLeaked:Activity com.TestCasesActivity 已经泄露了窗户 com.android.internal.policy.impl.PhoneWindow $ {DecorView 31a83555 最初添加的V.E ..... R ...... D 0,0-1026,348} 在android.view.ViewRootImpl。(ViewRootImpl.java:363) 在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:262) 在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 在android.app.Dialog.show(Dialog.java:298) at com.TestCasesActivity $ ConfigParser.onPreExecute(TestCasesActivity.java:41) 在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) 在android.os.AsyncTask.execute(AsyncTask.java:535) 在com.TestCasesActivity.onCreate(TestCasesActivity.java:30) 在android.app.Activity.performCreate(Activity.java:5984) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2254) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361) 在android.app.ActivityThread.access $ 800(ActivityThread.java:147) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1281) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5232) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
之前由解析器引起的另一次崩溃:
07-30 08:40:32.651 22982-23154/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.performance_tool, PID: 22982
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: org.w3c.dom.DOMException: Only one root element allowed
at org.apache.harmony.xml.dom.DocumentImpl.insertChildAt(DocumentImpl.java:421)
at org.apache.harmony.xml.dom.InnerNodeImpl.appendChild(InnerNodeImpl.java:52)
at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:306)
at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:128)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:107)
at com.XmlHelper.getNumberOfNodes(XmlHelper.java:29)
at com.TestCasesActivity$ConfigParser.doInBackground(TestCasesActivity.java:46)
at com.TestCasesActivity$ConfigParser.doInBackground(TestCasesActivity.java:33)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:1)
解决方案1: 您应该在onPostExecute中关闭进度对话框。像这样。
protected void onPostExecute(...){
// dismiss dialog here
dialog.dismiss();
}
你做了这一步吗?
解决方案2: 如果你在onPostExecute上执行了这一步,但仍然得到错误意味着请查看下面的下一个解决方案,
将你的上下文从onCreate方法传递给AsyncTask,就像这样,
confParser.execute(TestCasesActivity.this);
您的异步任务将如下所示,
private class ConfigParser extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
Context context;
public ConfigParser(Context context){
this.context=context;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(context);
dialog.setMessage("Loading TestCases Configuration...");
dialog.show();
}
@Override
protected void onPostExecute(...){
// dismiss dialog here
dialog.dismiss();
}
}
答案 1 :(得分:1)
我在我的项目中已经这样做了:
class CreaLista extends AsyncTask<String, Void, Void> {
private ProgressDialog dialog;
private IndiceActivity indiceActivity;
public CreaLista(IndiceActivity indiceActivity) {
this.indiceActivity = indiceActivity;
context = indiceActivity;
dialog = new ProgressDialog(context);
}
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Dati in caricamento");
this.dialog.show();
}
@Override
protected void onPostExecute(final Void success) {
if (dialog.isShowing()) {
dialog.dismiss();
}