2011-07-27 83 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

編輯:你的XML應該是這樣的,如果你不需要的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()在最後(我的錯......不能相信它)也沒有更多的Android崩潰,因爲我做了項目清理的事情。 – 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

不行也不行。順便說一句。我在以前的項目中使用了幾乎相同的代碼,並且在函數範圍之外使用findViewById時,一切正常。 – Rushino

+0

即使我刪除TextView myMessage =(TextView)findViewById(R.id.myMessage); Toast仍未執行。 – Rushino

1

這應該修復它。

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

沒有工作。即使是敬酒也不行。 – Rushino

相關問題