Android:从aleetdialog内的按钮打开alertdialog时失败

时间:2015-08-06 10:05:29

标签: android alertdialog

我想要一个alertDialog(里面有editText和spinner)来在数据库中添加一些数据。如果用户想要添加一个不在微调器中的值,我在微调器旁边添加了一些“新”按钮。这应该打开一个新的alertDialog,其中包含与新表相关的自定义字段。 一切工作都在我的“新”按钮的onClick()内部执行show()方法。 我的代码:

package com.dg
// import ...

public class MainActivity extends Activity
{
    // nouvelle classe :
    // ajouter dans listeDAO
    // ajoiter dans databasehandler
    // si champs.size!=2,6,7
    // implémenter refresh, liste_i
    // header_i,  ...

    // declarations

    public static final int MAX_SIZE_CHAMPS = 7;

    public Context context = null;

    public List<DAO> listeDAO = null;

    public List<Cursor> listeCursor = null;

    public AlertDialog alertDialogAjouter, alertDialogSupprimer = null;
    public AlertDialog.Builder alertDialogBuilderAjouter, alertDialogBuilderSupprimer = null;
    public LinearLayout customHeader, mainLayout = null;

    public List<EditText> listeEt = null;
    public List<Spinner> listeSp = null;
    public List<Button> listeBt = null

    public View dialogView = null;

// ...

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        context = this;

        // ...


        listeTv = new ArrayList<TextView>();
        listeEt = new ArrayList<EditText>();
        listeSp = new ArrayList<Spinner>();
        listeBt = new ArrayList<Button>();
        listeCursor = new ArrayList<Cursor>();

        LayoutInflater inflater = LayoutInflater.from(context);
        dialogView = inflater.inflate(R.layout.dialog, null);


        for (int index=0; index < MAX_SIZE_CHAMPS; index++)
        {
            listeTv.add((TextView)dialogView.findViewById(tabTv[index]));
            listeEt.add((EditText)dialogView.findViewById(tabEt[index]));
            listeSp.add((Spinner)dialogView.findViewById(tabSp[index]));
            listeBt.add((Button)dialogView.findViewById(tabBt[index]));
            listeBt.get(index).setOnClickListener(buttonNewListener);
            listeCursor.add(null);
        }



        // ...

        alertDialogBuilderAjouter = new AlertDialog.Builder(context);
        alertDialogBuilderAjouter.setTitle("ajout");
        alertDialogBuilderAjouter.setView(dialogView);
        alertDialogBuilderAjouter.setPositiveButton("ajouter", ajouterDialogButton);
        alertDialogBuilderAjouter.setNegativeButton("annuler", null);
        alertDialogAjouter = alertDialogBuilderAjouter.create();


    }


    public OnClickListener ajouterListener = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {

            DAO ajDAO = listeDAO.get(queueDAO.get(queueDAO.size() - 1));
            for (int i = 0; i < ajDAO.getNombreDeChamps(); i++)
            {
                valeursEntreesListe.add(new String());
            }
            prepareBox(ajDAO); // juste some opérations on textView, editText etc.
            alertDialogAjouter.show();
        }
    };

    public OnClickListener buttonNewListener = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            for (int i= 0; i<MAX_SIZE_CHAMPS; i++)
            {
                if (v.getId()==tabBt[i])
                {
                    // ...
            //      alertDialogAjouter.dismiss();
                    Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();
                    AlertDialog ad = null;
                    AlertDialog.Builder adb = new AlertDialog.Builder(context);
                    adb.setTitle("ajout");
                    adb.setView(dialogView);
                    adb.setPositiveButton("ajouter", ajouterDialogButton);
                    adb.setNegativeButton("annuler", null);
                    ad = adb.create();
                    DAO ajDAO = listeDAO.get(queueDAO.get(queueDAO.size() - 1));
                    for (int j = 0; j < ajDAO.getNombreDeChamps(); j++)
                    {
                        valeursEntreesListe.add(new String());
                    }
                    prepareBox(ajDAO);
                    finish();
                    ad.show(); // crash here!

                }
            }
        }
    };

    public DialogInterface.OnClickListener ajouterDialogButton = new DialogInterface.OnClickListener()
    {

        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            DAO ajDAO = null;
            if (queueDAO.size() > 0)
            {
                ajDAO = listeDAO.get(queueDAO.get(queueDAO.size() - 1));
            }

            if (ajDAO != null)
            {
                for (int i=0; i < ajDAO.getNombreDeChamps(); i++)
                {
                    if (!ajDAO.isFK(i))
                    {
                        valeursEntreesListe.set(i, (listeEt.get(i).getText().toString()));
                    }
                }
                valeursEntreesListe.set(ajDAO.getIndexPK(), "-1");
                try
                {
                    ajDAO.ajouter(ajDAO.createEntiteFromList(valeursEntreesListe));
                }
                catch (Exception e)
                {
                    Toast.makeText(context, "ajout : " + e.toString(), Toast.LENGTH_LONG).show();
                }
                refresh(ajDAO);
            }
            else if (ajDAO == null)
            {
                Toast.makeText(context, "Selectionner d'abord une table", Toast.LENGTH_LONG).show();
            }
        }
    };
}

对话框的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible">
        <Button
            android:id="@+id/bt0"
            style="@style/buttonEditFields"
            android:visibility="invisible"
            android:enabled="false"
        />
        <TextView
            android:id="@+id/tv1"
            android:text="tv 1 :"
            style="@style/textViewEditFields"
        />
        <EditText
            android:id="@+id/et1"
            android:hint="et1"
            android:layout_alignBottom="@id/tv1"
            android:layout_toRightOf="@id/tv1"
            style="@style/editTextEditFields"
        />
        <Spinner 
            android:id="@+id/sp1"
            style="@style/editTextEditFields"
            android:layout_alignBottom="@id/tv1"
            android:layout_toRightOf="@id/tv1"
        />
        <Button
            android:id="@+id/bt1"
            style="@style/buttonEditFields"
            android:layout_alignBottom="@id/tv1"
        />
        <TextView
            android:id="@+id/tv2"
            android:text="tv 2 :"
            android:layout_below="@id/tv1"
            style="@style/textViewEditFields"
        />
        <EditText
            android:id="@+id/et2"
            android:hint="et2"
            android:layout_alignBottom="@id/tv2"
            android:layout_toRightOf="@id/tv2"
            style="@style/editTextEditFields"
        />
        <Spinner 
            android:id="@+id/sp2"
            style="@style/editTextEditFields"
            android:layout_alignBottom="@id/tv2"
            android:layout_toRightOf="@id/tv2"
        />
        <Button
            android:id="@+id/bt2"
            style="@style/buttonEditFields"
            android:layout_alignBottom="@id/tv2"
        />
        <TextView
            android:id="@+id/tv3"
            android:text="tv 3 :"
            android:layout_below="@id/tv2"
            style="@style/textViewEditFields"
        />
        <EditText
            android:id="@+id/et3"
            android:hint="et3"
            android:layout_alignBottom="@id/tv3"
            android:layout_toRightOf="@id/tv3"
            style="@style/editTextEditFields"
        />
        <Spinner 
            android:id="@+id/sp3"
            style="@style/editTextEditFields"
            android:layout_alignBottom="@id/tv3"
            android:layout_toRightOf="@id/tv3"
        />
        <Button
            android:id="@+id/bt3"
            style="@style/buttonEditFields"
            android:layout_alignBottom="@id/tv3"
        />
        <TextView
            android:id="@+id/tv4"
            android:text="tv 4:"
            android:layout_below="@id/tv3"
            style="@style/textViewEditFields"
        />
        <EditText
            android:id="@+id/et4"
            android:hint="et4"
            android:layout_alignBottom="@id/tv4"
            android:layout_toRightOf="@id/tv4"
            style="@style/editTextEditFields"
        />
        <Spinner 
            android:id="@+id/sp4"
            style="@style/editTextEditFields"
            android:layout_alignBottom="@id/tv4"
            android:layout_toRightOf="@id/tv4"
        />
        <Button
            android:id="@+id/bt4"
            style="@style/buttonEditFields"
            android:layout_alignBottom="@id/tv4"
        />
        <TextView
            android:id="@+id/tv5"
            android:text="tv 5 :"
            android:layout_below="@id/tv4"
            style="@style/textViewEditFields"
        />
        <EditText
            android:id="@+id/et5"
            android:hint="et5"
            android:layout_alignBottom="@id/tv5"
            android:layout_toRightOf="@id/tv5"
            style="@style/editTextEditFields"
        />
        <Spinner 
            android:id="@+id/sp5"
            style="@style/editTextEditFields"
            android:layout_alignBottom="@id/tv5"
            android:layout_toRightOf="@id/tv5"
        />
        <Button
            android:id="@+id/bt5"
            style="@style/buttonEditFields"
            android:layout_alignBottom="@id/tv5"
        />
        <TextView
            android:id="@+id/tv6"
            android:text="tv 6 :"
            android:layout_below="@id/tv5"
            style="@style/textViewEditFields"
        />
        <EditText
            android:id="@+id/et6"
            android:hint="et6"
            android:layout_alignBottom="@id/tv6"
            android:layout_toRightOf="@id/tv6"
            style="@style/editTextEditFields"
        />
        <Spinner 
            android:id="@+id/sp6"
            style="@style/editTextEditFields"
            android:layout_alignBottom="@id/tv6"
            android:layout_toRightOf="@id/tv6"
        />
        <Button
            android:id="@+id/bt6"
            style="@style/buttonEditFields"
            android:layout_alignBottom="@id/tv6"
        />
        <TextView
            android:id="@+id/tv7"
            android:text="tv 7 :"
            android:layout_below="@id/tv6"
            style="@style/textViewEditFields"
        />
        <EditText
            android:id="@+id/et7"
            android:hint="et7"
            android:layout_alignBottom="@id/tv7"
            android:layout_toRightOf="@id/tv7"
            style="@style/editTextEditFields"
        />
        <Spinner 
            android:id="@+id/sp7"
            style="@style/editTextEditFields"
            android:layout_alignBottom="@id/tv7"
            android:layout_toRightOf="@id/tv7"
        />
        <Button
            android:id="@+id/bt7"
            style="@style/buttonEditFields"
            android:layout_alignBottom="@id/tv7"
        />
    </RelativeLayout>
</LinearLayout>

Logcat:

08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        FATAL EXCEPTION: main
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.view.ViewGroup.addView(ViewGroup.java:3249)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.view.ViewGroup.addView(ViewGroup.java:3225)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at com.android.internal.app.AlertController.setupView(AlertController.java:401)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at com.android.internal.app.AlertController.installContent(AlertController.java:241)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.app.AlertDialog.onCreate(AlertDialog.java:336)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.app.Dialog.dispatchOnCreate(Dialog.java:351)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.app.Dialog.show(Dialog.java:256)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at com.dg.MainActivity$100000006.onClick(MainActivity.java:356)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.view.View.performClick(View.java:4084)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.view.View$PerformClick.run(View.java:16987)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.os.Handler.handleCallback(Handler.java:615)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.os.Looper.loop(Looper.java:137)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at android.app.ActivityThread.main(ActivityThread.java:4794)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at java.lang.reflect.Method.invokeNative(Native Method)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at java.lang.reflect.Method.invoke(Method.java:511)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-06 11:57:16.990 14224 14224 E   AndroidRuntime com.dg                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)

我在这个网站上搜索但没有找到有用的东西。 我是新来的,所以如果我犯了错误,不要责怪我太多。 你能帮帮我吗?

编辑已经在show()

之前尝试了dismiss()

1 个答案:

答案 0 :(得分:2)

您将dialogView用于两个警报对话框。我认为这是问题所在。

第一个对话框设置dialogView的父级。当您尝试创建第二个对话框时,dialogView已经有父对象。

为第二个对话框创建不同的dialogView