简体   繁体   English

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

[英]Android : Fail while openning an alertdialog from a button inside an aleetdialog

I want an alertDialog ( with editText and spinner inside) to add some data in a database. 我想要一个alertDialog(内部带有editText和spinner)在数据库中添加一些数据。 I added some "new" button next to the spinner if the user wants to add a value that isn't in the spinner. 如果用户想要添加不在微调器中的值,则在微调器旁边添加了一些“新”按钮。 This should open a new alertDialog with custom fields related to a new table. 这将打开一个新的alertDialog,其中包含与新表相关的自定义字段。 Everything works exept the show() method inside the onClick() of my "new" button. 一切正常,而我的“新建”按钮的onClick()中的show()方法除外。 My code : 我的代码:

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

The layout of the dialog : 对话框的布局:

<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 : 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)

I searched on this site without finding something useful. 我在该网站上搜索时找不到有用的东西。 I'm new here, so if I made mistakes don't blame me too much. 我是新来的,所以如果我犯了错误,也不要怪我太多。 Could you help me ? 你可以帮帮我吗 ?


Edit already tried dismiss() before the show() 在show()之前编辑已经尝试过的dismiss()

You use dialogView for both alert dialogs. 您对两个警报对话框都使用dialogView I think this is the problem. 我认为这是问题所在。

The first dialog sets the dialogView 's parent. 第一个对话框设置dialogView的父级。 When you try to create the second dialog the dialogView already has a parent. 当您尝试创建第二个对话框时, dialogView已经有一个父级。

Create a different dialogView for the 2nd dialog. 为第二个对话框创建一个不同的dialogView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM