2011-12-22 114 views
0

目前,我的linearlayout在我的main.xml中設置爲GONE。一旦單選按鈕被選中,我需要將我的linearlayout的可見性設置爲visibile。但有錯誤的力量關閉。我的代碼有什麼問題嗎?Android - 選中單選按鈕後自動更改LinearLayout可見性

calc.java

public class calc extends Activity implements OnClickListener{ 

EditText et; 
RadioButton yes; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button submit =(Button)findViewById(R.id.submit); 
    submit.setOnClickListener(this); 

    final View linear = (EditText)findViewById(R.id.linear); 
    yes = (RadioButton)findViewById(R.id.option1); 
    yes.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 

     public void onCheckedChanged(CompoundButton buttonView, 
       boolean isChecked) { 

      if(yes.isChecked()) 
       linear.setVisibility(View.VISIBLE); 

     } 
    }); 
} 

public void onClick(View arg0) { 


} 

}

main.xml中

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

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

<RadioButton 
android:checked="false" 
android:id="@+id/option" 
android:text="No"/> 

<RadioButton 
android:checked="false" 
android:id="@+id/option1" 
android:text="Yes"/> 

</RadioGroup> 

<LinearLayout 
android:id="@+id/linear" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" 
android:layout_below="@+id/radioG" 
android:visibility="gone"> 

<TextView 
android:id="@+id/edit" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Please enter something: " 
/> 

<EditText 
android:id="@+id/edit" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" 
android:hint="numeric digits please" 
/> 
</LinearLayout> 

<Button 
android:id="@+id/submit" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" 
android:layout_below="@+id/linear" 
android:text="Submit" 
/> 

ERROR消息

Unable to start activity ComponentInfo{calc.calc/calc.calc.calc}: java.lang.ClassCastException: android.widget.LinearLayout 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4627) 
at java.lang.reflect.Method.invokeNative(Native Method) 

回答

2

您鑄造你的LinearLayoutEditText這裏:

final View linear = (EditText)findViewById(R.id.linear); 

取出演員和它應該工作。

+0

哈哈。我沒有意識到這一點。謝謝。它正在工作。 :D – Hend 2011-12-22 03:57:26

+0

請關閉此問題。 – 2011-12-22 04:01:26