如何在radiogroup中获取已选中按钮的ID?

时间:2012-11-16 13:22:31

标签: android nullpointerexception radio-button radio-group

我不知道为什么我得到null pointer - 例外?这就是它的起源:

int rgid = radioGroup.getCheckedRadioButtonId();

final Dialog dialog = new Dialog(MainSite.this);
dialog.setContentView(R.layout.upload_dialog);
dialog.setTitle("Upload - Einstellungen");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!

final EditText editname = (EditText) dialog.findViewById(R.id.filename);
editname.setText(filename);

//set up text
TextView tv_filesize = (TextView) dialog.findViewById(R.id.txt_filesize);
tv_filesize.setText("Dateigrösse: " + floata);             

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);

int rgid = radioGroup.getCheckedRadioButtonId();

if(rgid == R.id.radio_apps){
    dirpath = dirpath + "_Apps/";
}

if(rgid == R.id.radio_pictures){
    dirpath = dirpath + "_Bilder/";
}

if(rgid == R.id.radio_other){
    dirpath = dirpath + "_Sonstiges/";
}

//set up button
Button cmd_save = (Button) dialog.findViewById(R.id.cmd_save);
cmd_save.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        dialog.dismiss();
        final String[] file_save = {pathupload, editname.getText().toString(), dirpath};
        new UploadFile().execute(file_save);
    }

});

Button cmd_close = (Button) dialog.findViewById(R.id.cmd_close);
cmd_close.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        dialog.dismiss();
    }

});
//now that the dialog is set up, it's time to show it    
dialog.show();

upload_dialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<EditText
    android:id="@+id/filename"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginTop="16dp"
    android:hint="Dateiname"/>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="In folgenden Ordner hochladen:" />

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

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

    <RadioButton
        android:id="@+id/radio_pictures"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bilder" />

    <RadioButton
        android:id="@+id/radio_other"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sonstiges" />
</RadioGroup>

<TextView
    android:id="@+id/txt_filesize"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" />


<Button
    android:id="@+id/cmd_save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Datei hochladen" />

<Button
    android:id="@+id/cmd_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Schliessen" />

logcat的:

11-16 14:44:07.215: E/AndroidRuntime(23721): FATAL EXCEPTION: main
11-16 14:44:07.215: E/AndroidRuntime(23721): java.lang.NullPointerException
11-16 14:44:07.215: E/AndroidRuntime(23721):    at com.mseiz.give.your.apps.MainSite$5.onItemClick(MainSite.java:226)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3067)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at android.widget.AbsListView$1.run(AbsListView.java:3968)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at android.os.Handler.handleCallback(Handler.java:615)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at android.os.Looper.loop(Looper.java:137)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at android.app.ActivityThread.main(ActivityThread.java:4898)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at java.lang.reflect.Method.invokeNative(Native Method)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at java.lang.reflect.Method.invoke(Method.java:511)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
11-16 14:44:07.215: E/AndroidRuntime(23721):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

您收到NullPointerException,因为变量radioGroup为null。

这可能是因为:

  1. setContentView()设置的布局文件没有id为任何视图:R.id.radioGroup1
  2. 如果要动态创建视图层次结构,则不要将radioGroup视图添加到父ViewGroup。
  3. 最诚挚的问候,

    Anay

答案 1 :(得分:1)

使用您的代码为我工作

final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.activity_main);
        dialog.setTitle("Upload - Einstellungen");
        dialog.setCancelable(true);
        // there are a lot of settings, for dialog, check them all out!


        RadioGroup radioGroup = (RadioGroup)dialog.findViewById(R.id.radioGroup1); // set dialog.findViewById instead findViewById

        int rgid = radioGroup.getCheckedRadioButtonId();

        if (rgid == R.id.radio_apps) {
        }

        if (rgid == R.id.radio_pictures) {
        }

        if (rgid == R.id.radio_other) {
        }

        // set up button
        // now that the dialog is set up, it's time to show it
        dialog.show();

enter image description here