我有一个使用AsyncTask从服务器获取json文件的应用程序,因此显示了进度条。但是在显示进度条和方向更改时,应用程序崩溃显示VIEW NOT ATTACHED错误或HAS LEAKED WINDOW错误。
如何摆脱这些错误?否则,应用程序在AsyncTask上运行正常。
答案 0 :(得分:7)
那么在AsyncTask运行时“锁定”屏幕方向呢?!
例如:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
从服务器获取JSON文件之前
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
执行AsynTask后。
答案 1 :(得分:5)
我最好的猜测来自现有的有限信息:
当您旋转屏幕时,它会根据需要经历onPause(),onStop()和onDestroy()的正常活动生命周期过程,因此您必须在其中一个方法中关闭progressDialog
我是怎么做到的:
@Override
protected void onPause()
{
super.onPause();
if (mProgressDialog != null)
mProgressDialog.dismiss();
}
答案 2 :(得分:4)
试试这个活动。虽然不是很干净。它可以帮助
<activity android:configChanges="orientation|screenSize" android:name=".activity.MyActivity" ></activity>
答案 3 :(得分:3)
尝试我放在一起的这个小黑客...当你尝试各种屏幕更改组合并按下硬件后退键时,运行它并观察logcat信息。然后尝试下面评论中提到的调整并再修补一些。这可以让你避免使用android:configChanges =“orientation | screenSize”,这是不可取的。
这应该使用最小的manifest和layout.xml文件进行构建。如果我以某种方式将其弄糟,请留下评论。
// ********************************************************************
// *** AsynchHack - allows for multiple screen orientation changes, ***
// *** multiple asynch tasks are started / stopped,progress ***
// *** dialogs handled gracefully (no memory leaks / etc). ***
// *** ***
// *** Remove the wrapping comments in the onCreate() function ***
// *** to restrict processing to a single asynch instantiation ***
// *** screen rotation will dismiss the progress meter, but not ***
// *** the task ***
// *** ***
// *** View the logcat to understand the timing of events and ***
// *** their interactions ***
// *** ***
// ********************************************************************
package com.example.AsynchHack;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.List;
// *** Main activity
public class AsynchHack extends Activity {
final static String debugClass = "AsynchHack";
ProgressDialog mProgressDialog;
// *** standard onCreate()
public void onCreate(Bundle aSavedInstanceState) {
super.onCreate(aSavedInstanceState);
Log.d(debugClass, "onCreate(" + this + ")");
setContentView(R.layout.layout);
// As-is, this allows for multiple starts of asynch tasks
//if (aSavedInstanceState == null) {
mProgressDialog = new ProgressDialog(this);
(new AsynchStartup()).execute();
//}
}
// *** demo Asynch task
private class AsynchStartup extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
Log.d(debugClass,
"--- AsynchStartup() onPreExecute (" + this + ")");
mProgressDialog.setMessage("Loading please wait..");
mProgressDialog.show();
}
protected Void doInBackground(Void... aInput) {
Log.d(debugClass,
"--- AsynchStartup() doInBackground(" +
this + ") *starts*");
// simulate lengthy processing
try {
Thread.sleep(5000);
}
catch (InterruptedException aException) { }
Log.d(debugClass,
"--- AsynchStartup() doInBackground(" +
this + ") *finishes*");
return null;
}
protected void onProgressUpdate(Void aProgress){
}
protected void onPostExecute(Void aOutput) {
Log.d(debugClass,
"--- AsynchStartup() onPostExecute (" + this + ")");
if (mProgressDialog != null) {
Log.d(debugClass,
"--- AsynchStartup() onPostExecute " +
"(Dismissing progress meter)");
mProgressDialog.dismiss();
mProgressDialog = null;
}
else {
Log.d(debugClass,
"--- AsynchStartup() onPostExecute " +
"(Not required to dismiss progress meter)");
}
}
}
// *** standard onDestroy()
public void onDestroy() {
super.onDestroy();
Log.d(debugClass, "onDestroy(" + this + ")");
if (mProgressDialog != null) {
Log.d(debugClass,
"onDestroy(Dismissing progress meter)");
mProgressDialog.dismiss();
mProgressDialog = null;
}
else {
Log.d(debugClass,
"onDestroy(Not required to dismiss progress meter)");
}
}
}
答案 4 :(得分:1)
尝试使用此
dialog.setCancelable(false);