2015-01-31 60 views
-1

我试图继续创建我的应用程序(当您单击添加按钮时创建一个名称列表)。但我有这个例外,我没有成功地将其删除; 这是我的代码:错误:指定的孩子已经有父母

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) 

我已经认识到LL对父母的对话,但我不去除它赢得成功!

我都知道,使用一个ListView与吹气和适配器会更好,但我是一个初学者,我真的不明白什么是“吹气”和“适配器” ..

谢谢非常多

+0

请添加logcat跟踪。 – Rohit5k2 2015-01-31 21:45:26

+0

我刚加了:) – guillaumegg10 2015-01-31 21:49:33

+0

请看我的回答。为什么要在布局中添加对话框中的按钮?属于对话框。你不能这样做。如果你想添加按钮,你需要创建一个新的。 – Rohit5k2 2015-01-31 21:53:01

回答

0

这是必须导致问题的线。

ll.addView(validButton); 

您不能添加validButton布局,因为它已经在该对话框中,这意味着它有一个父。

删除此行,看看它是否工作。

+0

就是这样!我写了错误的按钮!非常感谢你 ! – guillaumegg10 2015-01-31 22:06:05

+0

很高兴能帮到你。请不要忘记接受它作为您的答案......:D – Rohit5k2 2015-01-31 22:06:56

相关问题