错误:指定的子项已有父项

时间:2015-01-31 21:41:50

标签: android

我正在尝试继续创建我的应用程序,(在单击“添加”按钮时创建名称列表)。但我有这个例外,我没有成功地删除它; 那是我的代码:

mageButton addButton = null;
RelativeLayout menuactivitylayout=null;

int j=0;



@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    menuactivitylayout=(RelativeLayout)findViewById(R.id.Relativelayoutacitivtymenu);


    addButton = (ImageButton) findViewById(R.id.buttonaddplayer);
    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Create custom dialog object
            final Dialog dialog = new Dialog(MenuActivity.this);
            dialog.setTitle(getResources().getString(R.string.addperson));
            // Include dialog.xml file
            dialog.setContentView(R.layout.dialoglayout);
            // Set dialog title
           // dialog.setTitle("Custom Dialog");

            // set values for custom dialog components - text, image and button
            final EditText text = (EditText) dialog.findViewById(R.id.editTextdialog);


            dialog.show();

            final ImageButton validButton = (ImageButton) dialog.findViewById(R.id.imageButtonvalid);
            // if decline button is clicked, close the custom dialog
            validButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final LinearLayout ll = new LinearLayout(MenuActivity.this);
                    ll.setOrientation(LinearLayout.HORIZONTAL);

                    ImageView person = new ImageView(MenuActivity.this);
                    person.setImageDrawable(getResources().getDrawable(R.drawable.person));
                    person.setMaxHeight(50);
                    person.setMaxWidth(50);
                    ll.addView(person);


                    final TextView name = new TextView(MenuActivity.this);
                    name.setText(text.getText().toString());
                    ll.addView(name);

                    final ImageButton edit = new ImageButton(MenuActivity.this);
                    edit.setId(j+1);
                    edit.setImageDrawable(getResources().getDrawable(R.drawable.edit));
                    edit.setLayoutParams(params);
                    j++;
                    edit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final Dialog editDialog = new Dialog(MenuActivity.this);
                            editDialog.setTitle("Editer");
                            dialog.setContentView(R.layout.edtidialoglayout);
                            final EditText edititextdialog = (EditText) dialog.findViewById(R.id.editTexteditDialog);


                            dialog.show();

                            ImageButton validButton = (ImageButton) dialog.findViewById(R.id.imageButtonvalideditdialog);
                            validButton.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    name.setText(edititextdialog.getText().toString());
                                    dialog.dismiss();

                                }
                            });


                        }





                    });

                    ll.addView(validButton);

                    menuactivitylayout.addView(ll);
                    dialog.dismiss();

                }
            });

这是logcat

01-31 22:33:49.587  20692-20692/com.example.guillaume.drinkwithfriends E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:3439)
        at android.view.ViewGroup.addView(ViewGroup.java:3310)
        at android.view.ViewGroup.addView(ViewGroup.java:3255)
        at android.view.ViewGroup.addView(ViewGroup.java:3231)
        at com.example.guillaume.drinkwithfriends.MenuActivity$1$1.onClick(MenuActivity.java:107)
        at android.view.View.performClick(View.java:4162)
        at android.view.View$PerformClick.run(View.java:17082)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
        at dalvik.system.NativeStart.main(Native Method)

我已经明白,我会为父母提供对话,但我没有成功删除它!

我知道使用带有inflater和适配器的Listview会更好,但我是初学者而且我并不真正理解什么是" Inflater"和"适配器" ..

非常感谢

1 个答案:

答案 0 :(得分:0)

这是导致问题的原因。

ll.addView(validButton);

您无法将validButton添加到布局中,因为它已在对话框中,这意味着它具有父级。

删除此行,看看它是否有效。