Android activity crashes when I try to use an AlertDialog and a custom Dialog

StackOverflow https://stackoverflow.com/questions/9038930

  •  03-12-2019
  •  | 
  •  

Domanda

I'm writing my first Android application and I have one activity that I'd like to display two separate dialogs:

Dialog A - A simple AlertDialog that shows text and gets dismissed Dialog B - A "Save As" pop-up with an EditText and Save and Cancel buttons

I've found tutorials on creating AlertDialogs and Custom Dialogs and I've been able to get each of them to work, but only separately. When I try to put all of the code into a switch/case statement in the onCreateDialog method, the application crashes when the AlertDialog is launched.

Here's my onCreateDialog code:

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        // Display dialog
        case 0:  
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setMessage(messageText);
            alertDialog.setNegativeButton(android.R.string.ok, 
                    new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                }
            });
            return alertDialog.create();    
        // Save As dialog

        case 1: 
            Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.save_as);
            dialog.setTitle("Save as:");

            Button cancel = (Button)findViewById(R.id.cancel);
            cancel.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                }

            });
            return dialog;  
        }
        return null;

    }

Either case will work by itself, but the application crashes when I put both cases in.

Here's the XML for the custom dialog layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:padding="10dp">

    <TextView 
        android:text="Save this list as:"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <EditText 
        android:id="@+id/list_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:drawable/bottom_bar" 
    android:paddingLeft="4.0dip"
    android:paddingTop="5.0dip" 
    android:paddingRight="4.0dip"
    android:paddingBottom="1.0dip" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
        <Button
           android:id="@+id/save" 
           android:layout_width="0.0dip"
        android:layout_height="fill_parent" 
        android:text="Save"
        android:layout_weight="1.0"
            ></Button>
        <Button
            android:id="@+id/cancel" 
            android:layout_width="0.0dip"
        android:layout_height="fill_parent" 
        android:text="Cancel"
        android:layout_weight="1.0"
            ></Button>

    </LinearLayout>

</LinearLayout>

Should I stick with just one format or the other? I've also read that DialogFragments are preferred now, but I haven't found any good novice-level tutorials on those yet. Any suggestions will be greatly appreciated.

È stato utile?

Soluzione

I eventually realized that I needed to pass data to and from the dialog, and I am targeting a low API so I just changed the Save As dialog to an Activity, and everything works fine. Learned a lot of limitations of dialogs along the way though....

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top