我有一个应用程序,它使用 AsyncTask 从服务器获取 json 文件,因此显示进度条。但是在显示进度条和更改方向时,应用程序崩溃并显示 VIEW NOT ATTACHED 错误或 HAS LEAKED WINDOW 错误。
如何摆脱这些错误?否则,应用程序在 AsyncTask 上运行良好。
我有一个应用程序,它使用 AsyncTask 从服务器获取 json 文件,因此显示进度条。但是在显示进度条和更改方向时,应用程序崩溃并显示 VIEW NOT ATTACHED 错误或 HAS LEAKED WINDOW 错误。
如何摆脱这些错误?否则,应用程序在 AsyncTask 上运行良好。
那么在 AsyncTask 运行时“锁定”屏幕方向怎么样?!
例如:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
就在您从服务器获取 JSON 文件之前
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
在你的 AsynTask 执行之后。
从目前可用的有限信息中,我的最佳猜测是:
当您旋转屏幕时,它会根据需要通过 onPause()、onStop() 和 onDestroy() 的正常活动生命周期过程,因此您必须在其中一种方法中关闭 progressDialog
我是怎么做到的:
@Override
protected void onPause()
{
super.onPause();
if (mProgressDialog != null)
mProgressDialog.dismiss();
}
试试这个为你的活动。虽然不是很干净。它可以帮助
<activity android:configChanges="orientation|screenSize" android:name=".activity.MyActivity" ></activity>
试试我整理的这个小技巧...运行它并在尝试各种屏幕更改组合并按下硬件返回键时观察 logcat 信息。然后尝试下面评论中提到的调整并进行更多修改。这可以让您避免 android:configChanges="orientation|screenSize" 这不是可取的。
这应该使用最小的清单和 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)");
}
}
}
尝试使用这个
dialog.setCancelable(false);