2011-06-06 21 views
0

私は、スピードとcurentの場所のようなデータを1トンクラスを使って読んでいます...私はそれらをループ内の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)); 
+0

どのようなエラーがありますか?たとえば、NullPointerExceptionまたはIllegalArgumentExceptionがあります。 – keyboardsurfer

+0

@Keyboardsurferそれは 'checkThread'で発生するので、おそらく' ConcurrentModificationException'です。 – Felix

+0

実際には、それは 'CalledFromWrongThreadException'です。 checkThreadが投げることができる唯一の例外です:http://google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/view/ViewRoot.java&l=2936 – Felix

答えて

3

あなたは常にUIの更新をUIThreadに投稿する必要があります。

runOnUiThread(new Runnable() { 
     public void run() { 
      speed_1.setText(Integer.toString(speed)); 
      location.setText(loc1); 

     } 
} 
3

Tヘルパーは、あなたが/メインUIスレッドと異なるスレッドから任意のUI操作を行うことができない、権利です。内側から、

基本的に

  • publishProgress
  • onProgressUpdate
  • :あなたはすでに AsyncTaskを使用しているので、あなたはそれが提供するメソッドを使用することができ、(素晴らしい作品)彼のソリューションを使用するか、どちらかあなたの doInBackgroundメソッドを publishProgress()と呼び、 AsyncTaskクラスはすべてのスレッド関連の頭痛を処理し、できるだけ早く onProgressUpdateをUIスレッドで呼び出します。UIを変更することができます(例: setText)。 。

関連する問題