【问题标题】:how to avoid force close when orientation is changed改变方向时如何避免强制关闭
【发布时间】:2013-02-27 12:39:11
【问题描述】:

我的应用需要从服务器获取数据。所以我在onPreExecute() 方法中显示ProgressDialog,在doInBackground() 方法中显示数据是从服务器加载的。

此时如果我改变方向,应用程序强制关闭。因为当前活动被销毁,doInBackground() 可能仍然引用旧活动。所以,我提到了this post,它讨论了同样的问题。但我不想使用android:configchanges,因为它不是首选。 androiddevelopersite 说这应该是最后的手段,而不是大多数应用程序的首选。

那么,有人可以用必要的代码 sn-ps 建议如何以我的应用不会强制关闭的方式处理这种情况吗?

Eidt:我的代码

    public class DataListActivity extends BaseActivity {

        private ArrayList<String> valueslist;

        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listlayout);
                     new LoadAsync().execute();

        }


         class LoadAsync extends AsyncTask<String, String, String> {

                /**
                 * Before starting background thread Show Progress Dialog
                 * */
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    progressDialog = new ProgressDialog(DataListActivity.this);
                    progressDialog.setMessage("Loading...");
                    progressDialog.setIndeterminate(false);
                    progressDialog.setCancelable(false);
                    progressDialog.show();
                }

                /**
                 * getting All datafrom url
                 * */
                protected String doInBackground(String... args) {

                    //Here I am doing httppost request.
                           try{ 
                            // looping through All data that I got from the server
                            for (int i = 0; i < subCategoriesJson.length(); i++) {
                                JSONObject jsonobj = subCategoriesJson.getJSONObject(i);

                                // Storing each json item in variable
                                String item = jsonobj.getString("data");

                                valueslist.add(item);

                            }
                            return "1";
                        }                    
                        else {

                            return null;
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }   
                    return null;
                }

   protected void onPostExecute(String msg) {

                    if( msg != null && msg.equals("1"))
                    {

                     progressDialog.dismiss();

                     runOnUiThread(new Runnable() {

                         public void run() {
                             //Updating parsed json data to Listview

                            ListView listView = (ListView)findViewById(R.id.list1);

            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item, valueslist);
                                listView.setAdapter(arrayAdapter); 

          listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
                            public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {


                                 String selectedthing = valueslist.get(position);

                                    }
                                }); 

                         }
                     });
                }else 
                {
                          //There is no data.
                }

                }

         }

    }

注意:我提供了这些信息,尽管这不是这个问题所必需的。我的活动有两个布局文件。一个用于portrait,另一个用于layout-land 文件夹中的landscape 模式。

下面是 logcat 窗口:

02-27 19:54:40.294: E/WindowManager(11710): Activity com.example.DataList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40555388 that was originally added here
02-27 19:54:40.294: E/WindowManager(11710): android.view.WindowLeaked: Activity com.example.prog.DataList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40555388 that was originally added here
02-27 19:54:40.294: E/WindowManager(11710):     at android.view.ViewRoot.<init>(ViewRoot.java:277)
02-27 19:54:40.294: E/WindowManager(11710):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-27 19:54:40.294: E/WindowManager(11710):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-27 19:54:40.294: E/WindowManager(11710):     at android.view.Window$LocalWindowManager.addView(Window.java:433)
02-27 19:54:40.294: E/WindowManager(11710):     at android.app.Dialog.show(Dialog.java:265)
02-27 19:54:40.294: E/WindowManager(11710):     at com.example.prog.DataList$LoadAsync.onPreExecute(DataList.java:77)
02-27 19:54:40.294: E/WindowManager(11710):     at android.os.AsyncTask.execute(AsyncTask.java:391)
02-27 19:54:40.294: E/WindowManager(11710):     at com.example.prog.DataList.onCreate(DataList.java:52)
02-27 19:54:40.294: E/WindowManager(11710):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
02-27 19:54:40.294: E/WindowManager(11710):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
02-27 19:54:40.294: E/WindowManager(11710):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
02-27 19:54:40.294: E/WindowManager(11710):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
02-27 19:54:40.294: E/WindowManager(11710):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
02-27 19:54:40.294: E/WindowManager(11710):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 19:54:40.294: E/WindowManager(11710):     at android.os.Looper.loop(Looper.java:150)
02-27 19:54:40.294: E/WindowManager(11710):     at android.app.ActivityThread.main(ActivityThread.java:4277)
02-27 19:54:40.294: E/WindowManager(11710):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 19:54:40.294: E/WindowManager(11710):     at java.lang.reflect.Method.invoke(Method.java:507)
02-27 19:54:40.294: E/WindowManager(11710):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-27 19:54:40.294: E/WindowManager(11710):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-27 19:54:40.294: E/WindowManager(11710):     at dalvik.system.NativeStart.main(Native Method)
02-27 19:54:40.294: E/ResourceType(11710): Style contains key with bad entry: 0x010102f3
02-27 19:54:40.294: E/ResourceType(11710): Style contains key with bad entry: 0x01010300
02-27 19:54:40.294: E/ResourceType(11710): Style contains key with bad entry: 0x0101039c

【问题讨论】:

  • 发布所有logcat数据...然后我们可以了解问题是什么或发生强制关闭时抛出了哪些异常。
  • 一些代码也可能有用。因为当我有一个活动的 ProgressDialog 时,我可以毫无问题地切换方向。

标签: android networking screen-orientation operation


【解决方案1】:

您必须在onPause() 中关闭您的对话框,如果需要,请在onResume() 中再次显示它。

您可能希望使用onRetainNonConfigurationInstance() 保留有关Activity 实例之间对话的一些状态,并从传递给onCreate() 的Bundle 中检索它。

【讨论】:

  • 感谢您的宝贵时间和 +1。我将用我的代码编辑我的问题。你能详细说明一下怎么做吗?
【解决方案2】:

您可以在等待来自服务器的数据时禁用方向更改。您可以暂时设置您的方向肖像:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

更多、清晰的解释请访问:How do I disable orientation change on Android?

【讨论】:

  • 我不想在运行时限制方向更改。此外,如果用户从横向开始然后更改为纵向怎么办?
  • @Sam 我想用户通常会以纵向模式打开 apk,但你是对的。但您也可以强制用户在开始从服务器下载数据之前以纵向模式启动活动。如果您在值班之前设置了方向,则用户无法将其更改为横向,直到您再次允许他。而且,如果需要,您可以检查方向,然后将其设置为横向或纵向。请查看访问:stackoverflow.com/questions/4697631/android-screen-orientation
  • 这是一种 hack,具有很多副作用。太多的开发人员禁用方向更改作为快速修复,以避免必须学习和编写 Activity 生命周期的代码。没有这个,除了方向变化之外,您将容易受到所有其他可能破坏和重新创建您的 Activity 的事情的影响。
【解决方案3】:

这与 Simon 给您的答案相同,但有一个代码示例。 必须检查进度是否完成,否则进度条会显示卡在100%。

@Override
public void onPause() {
    super.onPause();  // Always call the superclass method first

    proDialog.dismiss();
}

@Override
public void onResume(){
    super.onResume();


    proDialog.show();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 2017-01-20
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    相关资源
    最近更新 更多