开发者

Android read state of radiobuttons in custom dialog [update -> How to read from a custom dialog?]

-- updated question --

I can't read any data from my custom dialog, at first i thought it was just the radiobuttons, so i thought lets continue the project so i give a default value for the testing purpose.

Even after i've tried to read from the EditText, i get nullpointerexceptions.

Any idea what I'm doing wrong here?? (below is the XML + code and in the comments below i've shown what i did with the EditText)

-- old question --

So, i've used multiple sources to combine the layout/check functions I am using now.

When i'm trying to see what radiobutton was selected i keep getting nullpointer errors, so i've searched in here and i have added some test to see if i could get results by seeing what was set via radiogroup (instead of individual buttons).

Let me show you what i've done/tried so far.

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();

and the xml file i'm calling from: <?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>

The error shows up when i'm trying to read (one or two of) this lines (same line was shown since i've added log.v lateron)

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.

So to state the exa开发者_如何学Cct question again, how could i retrieve the selected radiobutton.

in example show in Log.v("selected button:", variable); = Verbose Androidblabla radio1


Johan... Might I suggest that you start by using the suggested architecture for creating and displaying a Dialog. So you would need to provide the method onCreateDialog(int id) and return the dialog in this method as in:

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;
    }

Where the password dialog is created as in:

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;
    }

Then show the dialog as in:

this.showDialog(MANAGE_PASSWORD);

Where:

private static final int MANAGE_PASSWORD= 0;

If you use the suggested architecture the Android OS will automagically handle orientation change so you do not want to launch a new dialog on each call to onCreate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜