Android 文本视图有问题
我使用一个吨级读取一些数据,如速度和当前位置……我使用循环中的AsyncTask线程读取它们……)……每次读取新的速度和位置时,我都在地图上方的文本视图中设置它们 我的代码的一部分是这样的:Android 文本视图有问题,android,textview,Android,Textview,我使用一个吨级读取一些数据,如速度和当前位置……我使用循环中的AsyncTask线程读取它们……)……每次读取新的速度和位置时,我都在地图上方的文本视图中设置它们 我的代码的一部分是这样的: while (true) { speed = ServerManager.getInstance().getLastSpeed(); loc1 = ServerManager.getInstance().getLastLocation(); speed = ServerManager
while (true) {
speed = ServerManager.getInstance().getLastSpeed();
loc1 = ServerManager.getInstance().getLastLocation();
speed = ServerManager.getInstance().getLastSpeed();
speed_1.setText(Integer.toString(speed));
location.setText(loc1);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:id="@+id/bar"
android:background="#FFFFFF"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/prod1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textSize="12px"
android:text="Current Location"
/>
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textColor="#013220"
android:textSize="12px"
/>
<TextView
android:id="@+id/prod2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="90dip"
android:textSize="12px"
android:text="Speed"
/>
<TextView
android:id="@+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textColor="#013220"
android:textSize="12px"
/>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/mapview1"
android:layout_width="fill_parent"
android:layout_below="@id/bar"
android:layout_height="wrap_content"
android:enabled="true"
android:clickable="true"
android:apiKey="0egkyrbiooKAfYyj43YB6wW1cmlG-TiIILXjpBg"
/>
</RelativeLayout>
其中:
- 速度是浮动的
- loc1是字符串
while (true) {
speed = ServerManager.getInstance().getLastSpeed();
loc1 = ServerManager.getInstance().getLastLocation();
speed = ServerManager.getInstance().getLastSpeed();
speed_1.setText(Integer.toString(speed));
location.setText(loc1);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:id="@+id/bar"
android:background="#FFFFFF"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/prod1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textSize="12px"
android:text="Current Location"
/>
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textColor="#013220"
android:textSize="12px"
/>
<TextView
android:id="@+id/prod2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="90dip"
android:textSize="12px"
android:text="Speed"
/>
<TextView
android:id="@+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textColor="#013220"
android:textSize="12px"
/>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/mapview1"
android:layout_width="fill_parent"
android:layout_below="@id/bar"
android:layout_height="wrap_content"
android:enabled="true"
android:clickable="true"
android:apiKey="0egkyrbiooKAfYyj43YB6wW1cmlG-TiIILXjpBg"
/>
</RelativeLayout>
我得到错误的那一行是:
speed_1.setText(Integer.toString(speed));
您应该始终在UIThread上发布UI更新
runOnUiThread(new Runnable() {
public void run() {
speed_1.setText(Integer.toString(speed));
location.setText(loc1);
}
}
您应该始终在UIThread上发布UI更新
runOnUiThread(new Runnable() {
public void run() {
speed_1.setText(Integer.toString(speed));
location.setText(loc1);
}
}
THelper是正确的,您不能从主/UI线程以外的其他线程执行任何UI操作。您可以使用他的解决方案(非常有效),也可以使用它提供的方法,因为您已经在使用
AsyncTask
:
- 出版进展
- onProgressUpdate
基本上,从
doInBackground
方法内部调用publishProgress()
,AsyncTask
类处理所有与线程相关的问题,并尽快在UI线程上调用onProgressUpdate
,确保可以修改UI(例如调用setText
)没有任何问题。THelper是正确的,您不能从主/UI线程以外的其他线程执行任何UI操作。您可以使用他的解决方案(非常有效),也可以使用它提供的方法,因为您已经在使用AsyncTask
:
- 出版进展
- onProgressUpdate
基本上,从
doInBackground
方法内部调用publishProgress()
,AsyncTask
类处理所有与线程相关的问题,并尽快在UI线程上调用onProgressUpdate
,确保可以修改UI(例如调用setText
)没有任何问题。这是什么错误?例如,它可能是一个NullPointerException或一个IllegalArgumentException。@Keyboardsurfer它很可能是一个ConcurrentModificationException
,因为它发生在checkThread
中实际上,它是一个从错误线程异常调用的。这是checkThread
可以抛出的唯一例外:然后我会使用@Thelper的答案。我所做的正是……另一个答案也很好……但我已经在使用publishProgress()发送UI地质点数据……以及我的新数据是如何浮动和字符串的……这将使我的publishProgress()变得复杂函数:)…并且使用Thelper的解决方案它可以工作:)这是什么样的错误?例如,它可能是一个NullPointerException或一个IllegalArgumentException。@Keyboardsurfer它很可能是一个ConcurrentModificationException
,因为它发生在checkThread
中实际上,它是一个从错误线程异常调用的。这是checkThread
可以抛出的唯一例外:然后我会使用@Thelper的答案。我所做的正是……另一个答案也很好……但我已经在使用publishProgress()发送UI地质点数据……以及我的新数据是如何浮动和字符串的……这将使我的publishProgress()变得复杂函数:)…并且使用Thelper的溶液它可以工作:)