Android getCheckedRadioButtonId只是给了我nullpoint异常
我有一些问题,我的XML中有两组单选按钮,它们在活动中有处理。它只是在第48行出现nullpoint异常时崩溃: int languangeId=languageGroup.getCheckedRadioButtonId 我已将XML中的默认选中按钮设置为buttongroup中的一个按钮, 那么为什么它不能得到一个有效的值呢Android getCheckedRadioButtonId只是给了我nullpoint异常,android,android-radiogroup,Android,Android Radiogroup,我有一些问题,我的XML中有两组单选按钮,它们在活动中有处理。它只是在第48行出现nullpoint异常时崩溃: int languangeId=languageGroup.getCheckedRadioButtonId 我已将XML中的默认选中按钮设置为buttongroup中的一个按钮, 那么为什么它不能得到一个有效的值呢 public class FirstTimeSelectMenu extends Activity{ private RadioGroup languageGroup;
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" />
首先,id@+id/languageGroup分配给哪个元素,因为它似乎没有在XML中声明!刚刚得到错误的未正确形成的代码…您使用的是片段吗?不是在这一点上。如果用户以前没有使用过该程序,那么它应该是一个初始活动,只运行一次。更深层的意图是使用片段的活动。您是否在考虑要查看的onclick?我对安卓系统还很陌生:将你的整个主要活动和xml都呈现出来。我的意思是,你的代码是这样构造的吗?第一个组有id languageGroup。编辑,哦,等等,为什么设置为facepalm下面的布局!这是您的全部XML代码吗?因为我在哪里都看不到android:id=@+id/languageGroup是的,这可能会导致你的问题。我真不敢相信这是一个如此愚蠢的错误,我的意思是我几个小时都在盲目地盯着我的代码看,却没有看到它。。。!谢谢: