2011-07-27 12 views
0

私は私が起こっているかを把握することはできませんenter image description here私のAndroidアプリケーションのクラッシュはなぜ分かりませんか?

(写真参照)エラーが発生します。文字通り私のRadioGroupはTextViewにキャストできないと言いますか?しかし、なぜ ? R.id.myMessageはRadioButtomではありません!

活動

public class Ch7_JoshRadioGroup extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final TextView myMessage = (TextView) findViewById(R.id.myMessage); 

     // Gestion du bouton "Hello, Joshua" 
     Button myButton = (Button) findViewById(R.id.myButton); 

     myButton.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       // View est un type générale 
       // v est le bouton dans ce contexte-ci 
       //Button thisOne = (Button) v; 
       //thisOne.setText("You pushed this button!"); 

       // Envoi un toast d'une longue durée lorsque le bouton est appuyé 
       Toast.makeText(Ch7_JoshRadioGroup.this, "test", Toast.LENGTH_LONG); 
      } 
     }); 
    } 
} 

レイアウト

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    android:id="@+id/myMessage" 
    /> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_joshua" 
    android:id="@+id/myButton" 
    /> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:background="#ff0000" 
     android:textColor="#000000" 
     android:text="red" 
    /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:background="#00ff00" 
     android:textColor="#000000" 
     android:text="green" 
    /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:background="#0000ff" 
     android:textColor="#000000" 
     android:text="blue" 
    /> 
</LinearLayout> 

<RadioGroup 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/myRadioGroup" 
    android:orientation="horizontal"> 

    <RadioButton 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="35dp" 
     android:layout_weight="1" 
     android:id="@+id/myRadioButtonRed" 
    /> 

    <RadioButton 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="35dp" 
     android:layout_weight="1" 
     android:id="@+id/myRadioButtonGreen" 
    /> 

    <RadioButton 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="35dp" 
     android:layout_weight="1" 
     android:id="@+id/myRadioButtonBlue" 
    /> 

</RadioGroup> 

</LinearLayout> 

答えて

1

あなたのxmlを削除してください。あなたが使用していない場合。

私はあなたがボタン

ボタンmyButtonという=(ボタン)findViewById(R.id.myButton)のテキストを設定したいと仮定。

myButton.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      myButton.seText("You pushed this button"); 

      // Envoi un toast d'une longue durée lorsque le bouton est appuyé 
      Toast.makeText(Ch7_JoshRadioGroup.this, "test", Toast.LENGTH_LONG); 
     } 

    } 

これを行う場合は、プロジェクトをクリーニングしてみてください。Project -> Clean select the project and hit OK

編集:あなたがこれはそれを修正する必要がありRADIOGROUP

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    android:id="@+id/myMessage" 
    /> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_joshua" 
    android:id="@+id/myButton" 
    /> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:background="#ff0000" 
     android:textColor="#000000" 
     android:text="red" 
    /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:background="#00ff00" 
     android:textColor="#000000" 
     android:text="green" 
    /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:background="#0000ff" 
     android:textColor="#000000" 
     android:text="blue" 
    /> 
</LinearLayout> 
</LinearLayout> 
+0

これは本当に奇妙なものになっています。最初のラジオボタンのテキストが変更されました。あなたはこのボタンをプッシュしました。何が大丈夫ですか?プロジェクトをクリーニングしていませんでした... – Rushino

+0

xmlの不要なコンポーネントを削除しましたか? –

+0

私はそれを動作させる方法を考え出しました。私は最後にshow()を忘れてしまったので、最初にトーストは実行されませんでした。(私のせいでそれを信じることはできません)また、プロジェクトのクリーンアップのことからアンドロイドクラッシュもなくなりました。 – Rushino

0

あなたは関数スコープ内findViewByIdを使うべきではないでしょうか。試してみてください:

// Gestion du bouton "Hello, Joshua" 
Button myButton = null; 

myButton.setOnClickListener(new OnClickListener(){ 
    @Override 
    public void onClick(View v) { 

     myButton = (Button) findViewById(R.id.myButton) 

     // View est un type générale 
     // v est le bouton dans ce contexte-ci 
     //Button thisOne = (Button) v; 
     //thisOne.setText("You pushed this button!"); 

     // Envoi un toast d'une longue durée lorsque le bouton est appuyé 
     Toast.makeText(Ch7_JoshRadioGroup.this, "test", Toast.LENGTH_LONG); 
    } 
}); 
+0

Nopeも機能しません。 Btw。以前のプロジェクトではほとんど同じコードを使用していましたが、関数スコープの外でfindViewByIdを使用するとすべてが正常に動作していました。 – Rushino

+0

Toastは、TextView myMessage =(TextView)findViewById(R.id.myMessage);を削除しても実行されません。 – Rushino

1

を必要といけない場合あなたのXMLは次のようになります。

Project->Clean->Clean All Projects [OK] 
Run->Run 
+0

動作しませんでした。トーストでさえも動かない。 – Rushino

関連する問題