setText,进入横向时重置并锁定
嘿,谢谢大家看完这篇文章。 我真的不明白为什么会发生这种情况,尽管它可能与我正在处理音频输入的后台线程有关。 一切正常,直到我倾斜手机,尚未放入横向肖像或类似的东西。所以它应该自动拉伸我的肖像布局。 但它只是拉伸它,它还重置我的标签,并使我无法在它们上使用我的 setText 功能。我还注意到它暂停了我的后台线程。 这是设置 textViews 的代码:
private void reactOnAudioMessage(Message msg) {
TextView txtVwLastValue = (TextView) findViewById(R.id.txtVwLastValue);
String result=null;
DataFile newData=null;
Bundle bndl=msg.getData();
// ** newData getting created, with the correcy value etc this is not the problem.
if(newData!=null){
txtVwLastValue.setText("Last Value Received:" +newData.getValue());
mDataList.add(newData);
drawGraph(mImageView);
}
}
这是我的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Record"
android:id="@+id/btnRecord">
</Button>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Last Value Received:" android:id="@+id/txtVwLastValue"></TextView>
<TextView android:text="TextView" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/txtVwType"></TextView>
<ImageView android:src="@drawable/icon" android:layout_width="fill_parent" android:id="@+id/imgViewGraph" android:layout_height="300px"></ImageView>
</LinearLayout>
Hey all thanks for looking this through.
I really can't understand why this happends, though it might have something to do with my background thread which is processing audioinput.
All works fine until i tilt the phone, havn't yet put an landscape portrait or stuff like that in. So it should automatic just stretch my portrait layout.
But it does just stretch it, it also resets my label, and makes me unable to use my setText features on them. I also noticed that it pauses my background thread.
Here is code to set the textViews:
private void reactOnAudioMessage(Message msg) {
TextView txtVwLastValue = (TextView) findViewById(R.id.txtVwLastValue);
String result=null;
DataFile newData=null;
Bundle bndl=msg.getData();
// ** newData getting created, with the correcy value etc this is not the problem.
if(newData!=null){
txtVwLastValue.setText("Last Value Received:" +newData.getValue());
mDataList.add(newData);
drawGraph(mImageView);
}
}
Here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Record"
android:id="@+id/btnRecord">
</Button>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Last Value Received:" android:id="@+id/txtVwLastValue"></TextView>
<TextView android:text="TextView" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/txtVwType"></TextView>
<ImageView android:src="@drawable/icon" android:layout_width="fill_parent" android:id="@+id/imgViewGraph" android:layout_height="300px"></ImageView>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您也看到的那样,这并不完全正确。未处理的方向更改的预期行为是重新创建 Activity(这意味着经历整个 Activity 生命周期、onDestroy()、onCreate() 等 - 显然这是重置标签的原因)。这可以通过多种方式处理:
http://developer.android.com /guide/topics/resources/runtime-changes.html &
http://developer.android.com/resources/articles/faster -screen-orientation-change.html
我在应用程序中以两种相当简单的方式“处理”方向变化。通过在清单中设置 screenOrientation 标志,将一个活动锁定为纵向(因为在横向中查看它没有意义):
我使用的第二个活动需要以两种方式查看,因为它包含一个在查看时看起来最好的图表在风景中。这基本上意味着我确保我的 Activity 正常关闭(包括我使用的任何线程或处理程序)并重新绘制以横向填充屏幕的图形。
您需要考虑您希望如何处理方向变化。什么对您的申请最有意义?有很多关于通过 Google 进行方向改变的优质资源。
我想您在后台线程中遇到的麻烦是由于您在活动中拥有的处理程序被破坏并重新创建的。
This is not entirely true, as you have also seen. The expected behaviour for an unhandled orientation change is to recreate the Activity (which means going through the entire Activity lifecycle, onDestroy(), onCreate() and so forth - obviously the reason your label is being reset). This can be handled in various ways:
http://developer.android.com/guide/topics/resources/runtime-changes.html &
http://developer.android.com/resources/articles/faster-screen-orientation-change.html
I "handle" orientation changes in 2 rather simple ways in my application. One activity is locked to portrait (as it makes no sense to view it in Landscape) by setting the screenOrientation flag in the Manifest:
The 2nd Activity that I use needs to be viewed in both ways as it contains a graph that looks best when viewed in Landscape. This basically means that I make sure that my Activity closes down gracefully (including any threads or handlers that I use) and is remade to draw the graph filling the screen in Landscape orientation.
You need to think about how you wish to handle the orientation change. What makes the most sense in your application? There's a lot of good resources around that talks about orientation changes through Google.
I imagine the trouble you are having with the background thread is due to handlers that you have in the Activity which is destroyed and remade.
当设备旋转时,它会有效地破坏您的活动并在新方向上重新创建您的活动。如果您将启动线程作为活动的一部分,那么这可以解释您所看到的一些行为。如果您希望线程在两个活动实例之间生存,那么您需要将其实现为服务而不是简单的线程。
证明这一点的方法是在 onCreate 方法中设置断点或添加一些日志记录,您将看到在应用程序启动和首次显示 Activity 时以及在设备旋转时都会发生此情况。
When the device is rotated, it effectively destroys and re-creates your activity in the new orientation. If you are kicking off your thread as part of your activity, that would explain some of the behaviour that you're seeing. If you want your thread to survive between the two activity instances, then you'll need to implement it as a Service rather than a simple thread.
The way to prove this is to set a breakpoint or add some logging in your onCreate method, and you'll see this being hit both when the app starts up and the activity is first displayed, and on device rotations.
每次倾斜手机时,即每次在纵向模式和横向模式之间切换时,活动都会重新启动,因此
onCreate()
被执行。Each time you tilt your phone, i.e each time you change between portrait mode and landscape mode, the activity restarts, so
onCreate()
is executed.