我正在尝试通过单击单选按钮来更改视图的可见性并编辑文本,但它不起作用。问题是java空指针。我该如何解决?我验证了所有变量都是正确的。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
View rad1 = (RadioButton)findViewById(R.id.radio1);
View rad2 = (RadioButton)findViewById(R.id.radio2);
final View test = findViewById(R.id.ProvencePrefecureView);
final EditText test1 = (EditText) findViewById(R.id.ProvencePrefecureEdit);
final View test2 = findViewById(R.id.rg);
final View test3 = (EditText) findViewById(R.id.Commune_arrondView);
final EditText test4 = (EditText) findViewById(R.id.Commune_arrondEdit);
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio1:
if (checked)
test.setVisibility(1);
test1.setVisibility(1);
test3.setVisibility(1);
test4.setVisibility(1);
break;
case R.id.radio2:
if (checked)
break;
}
}
}
XML:
<TextView
android:id="@+id/ProvencePrefecureView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/RegionView"
android:layout_below="@+id/RegionEdit"
android:layout_marginTop="16dp"
style="@style/question"
android:visibility="gone"
android:text="2. Province ou Préfecture : "
/>
<EditText
android:id="@+id/ProvencePrefecureEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ProvencePrefecureView"
android:layout_below="@+id/ProvencePrefecureView"
android:layout_marginTop="6dp"
style="@style/reponse"
android:visibility="gone"
android:ems="10"
/>
<TextView
android:id="@+id/Commune_arrondView"
android:layout_width="wrap_content"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/IDENTView"
style="@style/question"
android:layout_below="@+id/ProvencePrefecureEdit"
android:layout_marginTop="16dp"
android:text="3. Commune/Arrondissement : "
/>
<EditText
android:id="@+id/Commune_arrondEdit"
android:layout_width="fill_parent"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/NumeroDiscrictEdit"
android:layout_below="@+id/Commune_arrondView"
android:layout_marginTop="6dp"
style="@style/reponse"
android:ems="10"
/>