【问题标题】:Application Crashes on orientation change while showing Progress Bar in AsyncTask在 AsyncTask 中显示进度条时,应用程序在方向更改时崩溃
【发布时间】:2012-12-05 09:20:59
【问题描述】:

我有一个应用程序,它使用 AsyncTask 从服务器获取 json 文件并显示进度条。但是在显示进度条和更改方向时,应用程序崩溃并显示 VIEW NOT ATTACHED 错误或 HAS LEAKED WINDOW 错误

如何摆脱这些错误?否则应用程序在 AsyncTask 上运行良好。

【问题讨论】:

  • 显示代码以便我们提供帮助!
  • @Android-Developer :我有简单的 AsyncTask 类,只有 doInBackground onPreExecute 和 onPostExecute。
  • 所以你只需要关闭 onPause 上的进度对话框,如果 asynctask 仍在运行,请在你的 onResume() 中再次显示它;

标签: android crash android-asynctask screen-orientation


【解决方案1】:

那么在 AsyncTask 运行时“锁定”屏幕方向怎么样?!

例如:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

就在您从服务器获取 JSON 文件之前并

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

在你的 AsynTask 执行之后。

【讨论】:

  • 我也是这么想的。
  • +1 仅在您实际需要时强制用户进行配置。
  • 最好最简单的方法!!
  • 我被要求创建一个 setRequestedOrientation 方法。如何使用 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);当我的异步任务在不同的班级时。谢谢!
【解决方案2】:

从目前可用的有限信息中我的最佳猜测:

当您旋转屏幕时,它会酌情通过 onPause()、onStop() 和 onDestroy() 的正常活动生命周期过程,因此您必须在其中一种方法中关闭 progressDialog

我是怎么做到的:

@Override
protected void onPause()
{
    super.onPause();
    if (mProgressDialog != null)
        mProgressDialog.dismiss();
}

【讨论】:

    【解决方案3】:

    为您的活动试试这个。虽然不是很干净。它可以提供帮助

    <activity android:configChanges="orientation|screenSize" android:name=".activity.MyActivity" ></activity>
    

    【讨论】:

      【解决方案4】:

      试试我整理的这个小技巧...运行它并在尝试各种屏幕更改组合并按下硬件返回键时观察 logcat 信息。然后尝试下面的 cmets 中提到的调整并进行更多修改。这可以让您避免 android:configChanges="orientation|screenSize" 这不是可取的。

      这应该使用最小的清单和 layout.xml 文件按原样构建。如果我搞砸了,请留下 cmets。

      // ********************************************************************
      // *** 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)");
              }
      
          }
      
      }
      

      【讨论】:

        【解决方案5】:

        试试这个

          dialog.setCancelable(false);
        

        【讨论】:

        • 一些解释将有助于这个答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-08
        • 2020-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多