Android在自定义对话框中读取单选按钮的状态[更新 - >如何从自定义对话框中读取?]

时间:2011-03-20 10:47:41

标签: android dialog radio

- 更新的问题 -

我无法从自定义对话框中读取任何数据,起初我以为它只是radiobuttons,所以我想让我们继续项目,所以我给出了测试目的的默认值。

即使在我尝试从EditText中读取后,我也会获得nullpointerexceptions。

知道我在这里做错了什么? (下面是XML +代码,在下面的评论中,我展示了我对EditText做了什么)

- 老问题 -

所以,我使用了多个来源来组合我现在使用的布局/检查功能。

当我试图看到选择了什么单选按钮时,我一直得到nullpointer错误,所以我在这里搜索并添加了一些测试,看看我是否可以通过看看通过radiogroup设置的内容得到结果(而不是个别按钮)。

让我告诉你到目前为止我做过/尝试过的事情。

public class MainActivity extends Activity {

    private RadioGroup rbs; //added for test
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;

...

        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.connectpopup);
        dialog.setTitle("Quick Connect");
        dialog.setCancelable(true);
        Button button = (Button) dialog.findViewById(R.id.connect);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rbs = (RadioGroup) findViewById(R.id.widget33); //added for test
                rb1 = (RadioButton) findViewById(R.id.nmdc);
                rb2 = (RadioButton) findViewById(R.id.adc);
                rb3 = (RadioButton) findViewById(R.id.adcs);
                //RadioGroup.getCheckedRadioButtonId()
                Log.v("test", ""+rbs.getCheckedRadioButtonId()); //this doesn't give results either same nullpointer error
                if (rb1.isChecked() == true) { // these check for checked makes the app stop.
                    Toast.makeText(getApplicationContext(), rb1.getText(), 5).show();
                }
                if (rb2.isChecked() == true) {
                    Toast.makeText(getApplicationContext(), rb2.getText(), 5).show();
                }
                if (rb3.isChecked() == true) {
                    Toast.makeText(getApplicationContext(), rb3.getText(), 5).show();
                }
                dialog.dismiss();
            }
        });
        dialog.show();

和我正在调用的xml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/quickconnectpopup" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/help" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Please enter the address.\r\nSelect the protocol.\r\nEnter your nickname." > </TextView> <EditText android:id="@+id/hubaddress" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="some.address.com:411" android:textSize="18sp" > </EditText> <RadioGroup android:id="@+id/widget33" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/nmdc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="protocol1" > </RadioButton> <RadioButton android:id="@+id/adc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="prototocol2" > </RadioButton> <RadioButton android:id="@+id/adcs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="protocol3" > </RadioButton> </RadioGroup> <EditText android:id="@+id/nickname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Nick" android:textSize="18sp" > </EditText> <Button android:id="@+id/connect" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Connect" > </Button> </LinearLayout>

当我试图读取(一个或两个)这一行时出现错误(因为我已经添加了log.v,所以显示了相同的行)

10:26:44.814    1124    ERROR   AndroidRuntime  Uncaught handler: thread main exiting due to uncaught exception
10:26:44.845    1124    ERROR   AndroidRuntime  java.lang.NullPointerException
10:26:44.845    1124    ERROR   AndroidRuntime      at nl.my.project.MainActivity$1.onClick(MainActivity.java:89)

Log.v("test", ""+rbs.getCheckedRadioButtonId()); //this doesn't give results either same nullpointer error
if (rb1.isChecked() == true) { // these check for checked makes the app stop.

因此,要再次说出确切的问题,我该如何检索所选的单选按钮。

在示例中显示Log.v(“选中按钮:”,变量); = Verbose Androidblabla radio1

1 个答案:

答案 0 :(得分:0)

Johan ......我建议您先使用建议的架构来创建和显示Dialog。因此,您需要提供onCreateDialog(int id)方法并返回此方法中的对话框,如下所示:

protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch(id) {
        case MANAGE_PASSWORD:
            dialog= getInstancePasswordDialog(); //<== CREATE DIALOG HERE
            break;
        case DIALOG_ABOUT:
            // do the work to define the About Dialog
            dialog= getInstanceAlertDialog(); // called "the first time"
            break;
        default:
            dialog = null;
            break;
        }
        return dialog;
    }

创建密码对话框的地方如下:

private Dialog getInstancePasswordDialog() {
        final Dialog d= new Dialog(this);
        d.setContentView(R.layout.password_dialog);
        d.setTitle("Key Manager");
        final EditText editTextPasswordFirst= (EditText)d.findViewById(R.id.EditTextPasswordFirst);
        final EditText editTextPasswordSecond= (EditText)d.findViewById(R.id.EditTextPasswordSecond);
        // CANCEL BUTTON HANDLER
        final Button buttonCancel= (Button)d.findViewById(R.id.ButtonPasswordCancel);
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                d.dismiss();
            }
        });
        // UPDATE BUTTON HANDLER
        final Button buttonUpdate= (Button)d.findViewById(R.id.ButtonPasswordUpdate);
        buttonUpdate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String entryFirst= editTextPasswordFirst.getText().toString();
                String entrySecond= editTextPasswordSecond.getText().toString();       
                if (entryFirst.equals(entrySecond)) { // do NOT use == with string values
                    if (entryFirst.length() != lengthPassword) { // invalid key length
                        editTextPasswordFirst.setText("Invalid Key Length of "+
                                new Integer(entryFirst.length()).toString());                       
                        editTextPasswordSecond.setText("");
                        editTextPasswordFirst.selectAll();
                        editTextPasswordFirst.requestFocus();
                    }
                    else {
                        editTextPassword.setText(entryFirst);
                        // *** Clear Key Fields ***
                        editTextPasswordFirst.setText("");                  
                        editTextPasswordSecond.setText("");
                        d.dismiss();
                    }
                }
                else { // entries do not match
                    editTextPasswordFirst.setText("Entries did not match!");                  
                    editTextPasswordSecond.setText("");
                    editTextPasswordFirst.selectAll();
                    editTextPasswordFirst.requestFocus();
                }
            }
        });
        return d;
    }

然后按以下方式显示对话框:

this.showDialog(MANAGE_PASSWORD);

其中:

private static final int MANAGE_PASSWORD= 0;

如果您使用建议的架构,Android操作系统将自动处理方向更改,因此您不希望在每次调用onCreate时启动新对话框。