2011-12-09 12 views
2

私はアンドロイドが新しく、プログラムを作成していますので、別のスレッドとハンドラを使用してUIを更新します。 TextViewが1つしかない場合は機能しますが、テキストビューを追加すると機能しません。ハンドラを使用してグループTextViewsを更新すると失敗しました。

マイOnCreateの

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    tvSpeed = (TextView) findViewById(R.id.SpeedView02); 
    tvHeart = (TextView) findViewById(R.id.HeartRate02); 
    tvGrade = (TextView) findViewById(R.id.Grandient02); 

    btnConnect = (Button) findViewById(R.id.BtnConnect); 

    btAdapter = BluetoothAdapter.getDefaultAdapter(); 

    if (btAdapter == null) { 
     Toast.makeText(this, 
      "Bluetooth is not supported on this hardware platform", 
      1000).show(); 
     onDestroy(); 
    } 
} 

マイハンドラ

private final Handler handler = new Handler() { 

    private int current; 

    public void handleMessage(Message msg) { 

     current = (Integer) msg.obj; 

     switch(msg.what) 
     { 
     case 1: 
      tvSpeed.setText(current + "Km/h"); 
      break; 
     case 2: 
      tvHeart.setText(current + "Bps"); 
      break; 
     default: 
      break; 
     }   
     tvGrade.setText(0); // if I remove this, it works. 
    } 
}; 

私のmain.xml

代わりに、ハンドラの
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout android:id="@+id/LinearLayout01" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"> 
     <TextView android:id="@+id/SpeedView01" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="速度:"></TextView> 
     <TextView android:id="@+id/SpeedView02" android:layout_width="70dip" 
      android:layout_height="wrap_content" 
      android:text=""></TextView> 
     <TextView android:id="@+id/HeartRate01" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="心率:"></TextView> 
     <TextView android:id="@+id/HeartRate02" android:layout_width="70dip" 
      android:layout_height="wrap_content" 
      android:text=""></TextView> 
     <TextView android:id="@+id/Gradient01" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="坡度:"></TextView> 
     <TextView android:id="@+id/Grandient02" android:layout_width="70dip" 
      android:layout_height="wrap_content" 
      android:text=""></TextView> 
    </LinearLayout> 

    <LinearLayout android:orientation="horizontal" 
     android:layout_width="match_parent" android:layout_height="wrap_content" > 
     <Button android:id="@+id/BtnConnect" android:layout_width="match_parent" 
      android:layout_height="wrap_content" android:text="Connect" 
      android:layout_weight="1.0" android:onClick="onConnectButtonClicked" /> 
     <Button android:id="@+id/BtnQuit" android:layout_width="match_parent" 
      android:layout_height="wrap_content" android:text="Exit" 
      android:layout_weight="1.0" android:onClick="onQuitButtonClicked" /> 
    </LinearLayout> 

</LinearLayout> 
+0

BTW:onDestroy()を直接呼び出さないで、代わりにfinish()を呼び出します。 –

答えて

3

setText(0);

あなたは明らかに、0が有効な値になることはありません

、それはprolly「見つかりませんリソースを」投げ... setText(int)が有効なR.string.XXX値を期待する関数であると認識します例外。

+0

+1 - 非常に良いキャッチ –

+0

+1、正確にキャッチ。素敵なもの.. – user370305

+0

ありがとう、私はそれを修正!私はsetText(int)がResourceIdを期待する関数であることを認識していません –

0

、私はあなたがでPainless Threadingとして知られているAsyncTaskを使用することをお勧めしますアンドロイド。 doInBackground()インサイド

  1. 、あなたが値を持っていると、あなたはonPostExecute()メソッド内のUIのupdationを作ることができます
  2. すべてのバックグラウンドタスクを実装します。
関連する問題