当我改变屏幕方向我的Andr​​oid应用程序崩溃 [英] My Android application crashes when I change screen orientation

查看:111
本文介绍了当我改变屏幕方向我的Andr​​oid应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,网络访问是在一个线程中运行。每当我改变屏幕的方向,我的应用程序崩溃。我会如何解决这个问题?

In my application, network access is running in a thread. Whenever I change screen orientation, my application crashes. How might I resolve this?

推荐答案

你可以有你的应用程序侦听会导致重启事件 - 像方向和键盘可见性的变化 - 和你的活动范围内处理它们。

you can have your application listen for events that would cause a restart – like orientation and keyboard visibility changes – and handle them within your Activity.

加入 Android的开始:configChanges 节点到您的活动的清单节点

Start by adding the android:configChanges node to your Activity's manifest node

 android:configChanges="keyboardHidden|orientation"

那么活动中覆盖onConfigurationChanged方法,并调用的setContentView迫使GUI布局在新的方向来重新做。

Then within the Activity override the onConfigurationChanged method and call setContentView to force the GUI layout to be re-done in the new orientation.

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.myLayout);
}

这篇关于当我改变屏幕方向我的Andr​​oid应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆