0

I have action bar drop-down in my android application. When user click a item from the drop-down. I added a AlertDialogBuilder to get some user data from user, if user press ok it will save the data. When I run the code I got this exception. IMAGE_LOG

this is the code I'm using

@Override
public boolean onNavigationItemSelected(final int i, long l) {


    final ArrayList<Host> hosts = HostFactory.getHosts(this.getApplicationContext());

    // get prompts.xml view
    LayoutInflater li = LayoutInflater.from(this);
    View promptsView = li.inflate(R.layout.auto_discovery_prompt, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            this);

    // set prompts.xml to alertdialog builder
    alertDialogBuilder.setView(promptsView);

    final EditText txtDeviceName = (EditText) promptsView.findViewById(R.id.txtdevicename_editTextDialogUserInput);
    final EditText txtUsername = (EditText) promptsView.findViewById(R.id.txtusername_editTextDialogUserInput);
    final EditText txtPassword = (EditText) promptsView.findViewById(R.id.txtpassword_editTextDialogUserInput);
    final EditText txtPort = (EditText) promptsView.findViewById(R.id.txtport_editTextDialogUserInput);
    String mac=navSpinner.get(i).getMacaddress();

    txtDeviceName.setText(navSpinner.get(i).getTitle());
    txtPort.setText(navSpinner.get(i).getPort());
    for(SpinnerNavItem items:navSpinner)
    {
        if(items.getTitle().toString()!="Select your Device" )
        {

            // set dialog message
            alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                @TargetApi(Build.VERSION_CODES.GINGERBREAD)
                                public void onClick(DialogInterface dialog,
                                                    int id) {
                                    // get user input and set it to result
                                    // edit text
                                    //result.setText(userInput.getText());

                                    String dd=txtPassword.getText().toString();


                                    Host savecurrent_selected_host =new Host();

                                    savecurrent_selected_host.name=txtDeviceName.getText().toString();
                                    savecurrent_selected_host.addr=navSpinner.get(i).getIPaddress().toString();

                                    try {
                                        savecurrent_selected_host.port = Integer.parseInt(txtPort.getText().toString());
                                    } catch (NumberFormatException e) {
                                        savecurrent_selected_host.port = Host.DEFAULT_HTTP_PORT;
                                    }

                                    savecurrent_selected_host.user=txtUsername.getText().toString();
                                    savecurrent_selected_host.pass=txtPassword.getText().toString();

                                    savecurrent_selected_host.esPort = Host.DEFAULT_EVENTSERVER_PORT;
                                    savecurrent_selected_host.timeout = Host.DEFAULT_TIMEOUT;
                                    savecurrent_selected_host.access_point="";
                                    if(!(navSpinner.isEmpty()&& navSpinner.get(i).getMacaddress().toString().isEmpty()))
                                        savecurrent_selected_host.mac_addr=navSpinner.get(i).getMacaddress().toString();
                                    else
                                        savecurrent_selected_host.mac_addr="";
                                    savecurrent_selected_host.wol_port = Host.DEFAULT_WOL_PORT;
                                    savecurrent_selected_host.wol_wait = Host.DEFAULT_WOL_WAIT;

                                    HostFactory.addHost(getApplicationContext(), savecurrent_selected_host);
                                    HostFactory.saveHost(getApplicationContext(),savecurrent_selected_host);

                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int id) {
                                    dialog.cancel();

                                }
                            });

            // create alert dialog
            alertDialog = alertDialogBuilder.create();

            // show it
            if(!alertDialog.isShowing())
                alertDialog.show();

    }
}

How can I fix this problem?( application is braking when come to alertDialog.show() )

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Nuwan Indika
  • 901
  • 4
  • 14
  • 27

1 Answers1

1

I believe you have two issues with your code, and the first issue is causing the second. The first issue is that you are displaying a new alertDialog for each item in navSpinner. The following if statement

if(items.getTitle().toString()!="Select your Device" )

will always be true because you are doing reference comparison (==). You want to use .equals(). (See here for a better explanation).

if(!items.getTitle().toString().equals("Select your Device" ))

Even this seems a little fishy, as you probably want to compare the loop item to the selected item, not some default string. Anyways, as a result, you are creating a new alertDialog for each loop iteration

alertDialog = alertDialogBuilder.create();

create() returns a new dialog each time, so the following check

if(!alertDialog.isShowing())

is always true, as the new dialog which was created one line above has obviously not been shown yet. The means the new alertDialog is always shown

alertDialog.show();

On first glance, you might expect this to result in multiple alert dialogs being shown, one for each item in the loop. However, this will not be the case, as the second alert dialog - which will be created during the second iteration of the loop - will cause a IllegalStateException. Why? Because of this:

alertDialogBuilder.setView(promptsView);

Although your second dialog truly is its own dialog, it shares the promptsView with the dialog that was created during the first loop iteration. When show() is called, promptsView is given a parent. This is OK when the first dialog is shown, because promptsView is a new view without a parent assigned. However, when the second dialog is show - from the second loop iteration - the show() method attempts to give promptsView a parent, but this time it fails because it has already been given a parent from the first dialog show(). And thus the error: The specified child already has a parent

In short, fix your loop so that only one dialog is displayed, and you should be good to go.

Community
  • 1
  • 1
pathfinderelite
  • 3,047
  • 1
  • 27
  • 30