getCheckedRadioButtonId只给我空指针异常

时间:2014-11-19 23:34:23

标签: android android-radiogroup

我有一些问题,我的XML中有两组Radiobuttons,它们在一个活动中处理。它只是在第48行遇到了nullpoint异常: int languangeId = languageGroup.getCheckedRadioButtonId();

我已将XML中的“默认”选中按钮设置为buttongroup中的一个按钮, 那么为什么它没有得到有效的价值?:(

public class FirstTimeSelectMenu extends Activity{

private RadioGroup languageGroup;
private RadioGroup storageGroup;
private Button okButton;
private String getStorage;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_time_run);

    languageGroup = (RadioGroup)this.findViewById(R.id.languageGroup);
    storageGroup = (RadioGroup)this.findViewById(R.id.storageGroup);
    okButton = (Button)findViewById(R.id.okBtn);



    okButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {




            int languangeId = languageGroup.getCheckedRadioButtonId();
            int storageId = storageGroup.getCheckedRadioButtonId();
            RadioButton languageb = (RadioButton)findViewById(languangeId);
            RadioButton storageb = (RadioButton)findViewById(storageId);

            if(languageb.equals("Norwegian")){
                    //Need to fix this!
            }
            if(languageb.equals("English")){
                    //Need to fix this!
            }
            if(storageb.equals("SD Card")){
               String sdStoragePath = Environment.getExternalStorageDirectory().toString() + "/PictureTalk/";
               FileInteraction fi = new FileInteraction();
               fi.firstTimeFillPath(getResources(), "PictureTalk/Food", sdStoragePath +"PictureTalk/Food");
               fi.firstTimeFillPath(getResources(), "PrivatePictures", Environment.getExternalStorageDirectory().toString() +"PrivatePictures");
               Intent intent = new Intent(getApplicationContext(),MainMenuActivity.class);
               intent.putExtra("dataStorePath",sdStoragePath);
               startActivity(intent);
            }
        }
    });
}}

我的XML文件

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

<TextView
    android:id="@+id/languagetext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="236dp"
    android:text="Choose language" />

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/languagetext"
    android:layout_alignRight="@+id/languagetext"
    android:layout_below="@+id/languageGroup">



    <RadioButton
        android:id="@+id/norwegianRadioBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Norwegian" />

    <RadioButton
        android:id="@+id/englishRadioBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="English" />

</RadioGroup>


<TextView
    android:id="@+id/internal_sd"
    android:layout_width="464dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="236dp"
    android:text="Do you want to store the data to:" />

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:id="@+id/storageGroup">



    <RadioButton
        android:id="@+id/sdcardRadioBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:checked="true"
        android:text="SD card" />


    <RadioButton
        android:id="@+id/internalRadioBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Internal storage" />
</RadioGroup>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ok"
    android:id="@+id/okBtn"
    android:layout_gravity="bottom" />

1 个答案:

答案 0 :(得分:0)

首先,为哪个元素分配了标识"@+id/languageGroup",因为它似乎没有在您的XML中声明