Чета някои данни като скорост и текущо местоположение, използвайки един клас тон.... Чета ги, използвайки AsyncTask нишка в цикъл....:).... всеки път, когато чета нов скорост и местоположение, задавам ги в текстов изглед над карта.
Ето как изглежда част от моя код:
while (true) {
speed = ServerManager.getInstance().getLastSpeed();
loc1 = ServerManager.getInstance().getLastLocation();
speed = ServerManager.getInstance().getLastSpeed();
speed_1.setText(Integer.toString(speed));
location.setText(loc1);
}
където:
- скорост-е плаващ и
- loc1-е низ.
Ето как изглежда моят xml:
<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>
И това е грешката, която получавам в моя 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)
Редът, на който получавам грешката, е:
speed_1.setText(Integer.toString(speed));
ConcurrentModificationException
, тъй като се среща вcheckThread
- person Felix   schedule 06.06.2011CalledFromWrongThreadException
. Това е единственото изключение, коетоcheckThread
може да хвърли: google.com/codesearch/p?hl=bg#uX1GffpyOZk/core/java/android/ - person Felix   schedule 06.06.2011runOnUiThread()
, то е по-малко ефективно. Предаването на задачи между нишки като това е доста сложно във фонов режим, така че е по-добре да го сведете до минимум. Не казвам, че трябва да приемете моето решение, но трябва да сте наясно с това, в случай че се превърне в проблем. - person Felix   schedule 06.06.2011