使用单选按钮更改视图可见性时的NPE

时间:2013-10-30 21:41:33

标签: android

我正在尝试通过单击单选按钮来更改视图的可见性和编辑文本,但它不起作用。问题是java null指针。我该如何解决?我确认所有变量都是正确的。

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"
    />

1 个答案:

答案 0 :(得分:0)

Java中的所有代码都必须在方法中。试试这个:

final View rad1;
final View rad2;

final View test;
final EditText;
final View test2;
final View  test3;
final EditText  test4;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    rad1 = (RadioButton)findViewById(R.id.radio1);
    rad2 = (RadioButton)findViewById(R.id.radio2);

    test = findViewById(R.id.ProvencePrefecureView);
    test1 = (EditText) findViewById(R.id.ProvencePrefecureEdit);
    test2 = findViewById(R.id.rg);
    test3 = (EditText) findViewById(R.id.Commune_arrondView);
    test4 = (EditText) findViewById(R.id.Commune_arrondEdit);

}