problem with a text view
I'm reading some data like speed and curent location using a single ton class....I'm reading them using an AsyncTask thread in a loop....:)....each time I'm reading a new speed and location I'm setting them in a text view above a map.
This is how a part of my code looks like:
while (true) {
speed = ServerManager.getInstance().getLastSpeed();
loc1 = ServerManager.getInstance().getLastLocation();
speed = ServerManager.getInstance().getLastSpeed();
speed_1.setText(Integer.toString(speed));
location.setText(loc1);
}
where:
- speed-is float and
- loc1-is String.
This is how my xml looks like:
<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>
And this is the error I get posted in my logcat:
at android.view.ViewRoot.checkThread(ViewRoot.java:2683)
at android.view.ViewRoot.requestLayout(ViewRoot.java:557)
at android.view.View.requestLayout(View.java:7918)
at android.view.View.requestLayout(View.java:7918)
at android.view.View.requestLayout(View.java:7918)
at android.view.View.requestLayout(View.java:7918)
android.widget.RelativeLayout.requestLayout(RelativeLayout.java:255)
at android.view.View.requestLayout(View.java:7918
at android.view.View.requestLayout(View.java:7918)
at android.widget.TextView.checkForRelayout(TextView.java:5380)
开发者_开发问答
at android.widget.TextView.setText(TextView.java:2684)
at android.widget.TextView.setText(TextView.java:2552)
at
android.widget.TextView.setText(TextView.java:2527)
at com.Server_1.Server4$InitTask.doInBackground(Server4.java:86)
at com.Server_1.Server4$InitTask.doInBackground(Server4.java:1)
The line at which I get thie error is:
speed_1.setText(Integer.toString(speed));
You should always post UI updates on the UIThread.
runOnUiThread(new Runnable() {
public void run() {
speed_1.setText(Integer.toString(speed));
location.setText(loc1);
}
}
THelper is right, you cannot do any UI operations from a different thread than the main / UI one. You can either use his solution (which works great) or, since you are already using an AsyncTask
you can use the methods provided by it:
- publishProgress
- onProgressUpdate
Basically, from inside your doInBackground
method you call publishProgress()
and the AsyncTask
class handles all thread-related headaches and calls your onProgressUpdate
as soon as possible, on the UI thread, ensuring that you can modify the UI (for example call setText
) without any problems.
精彩评论