Question

I am getting an object of RadioButton by using findViewById() and then setting an `onclicklistener' for it.The code goes like :

final EditText editTextView = (EditText)findViewById(2001);
RadioButton rb = (RadioButton) findViewById(R.id.radio3);
        rb.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {


                LinearLayout linearLayout = (LinearLayout) findViewById(R.id.editTextGroupLayout);
                LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
                editTextView.setLayoutParams(params);
                editTextView.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL| InputType.TYPE_NUMBER_FLAG_SIGNED);
                editTextView.setId(2001);
                linearLayout.addView(editTextView);




             }
         });

When i click Once on the radiobutton it works fine.But when i click it twice,it generates

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

By clicking it twice why this exception occurs?

What i tried

I tried to remove the line final EditText editTextView = (EditText)findViewById(2001); and add this line inside the onClick(),EditText editTextView = (EditText)findViewById(2001);.But by doing this it doesn't get executed even once.It shows exception too.

Was it helpful?

Solution

It is beacouse you got only one instance of edittext which alerady has a parent after first click. Try to remove view from parent and than place it again. Or maybe try to show/hide it?

You should propably do something like this.

linearLayout.removeAllViews(); or linearLayout.removeView(editTextView);
...
linearLayout.addView(editTextView);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top