目前,我的linearlayout
在GONE
中设为main.xml
。检查单选按钮后,我需要将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"
/>
错误消息
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)
答案 0 :(得分:2)
您将LinearLayout
投射到EditText
这里:
final View linear = (EditText)findViewById(R.id.linear);
删除演员,它应该有用。