Reputation: 2862
I am inflating a layout dynamically. I want to add child views in that layout. I tried to add this but getting exception:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first
Event function:
private void createEvent(LayoutInflater inflater, ViewGroup dayplanView, int fromMinutes, int toMinutes, String title,String location) {
final View eventView = inflater.inflate(R.layout.event_view, dayplanView, false);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();
RelativeLayout container = (RelativeLayout) eventView.findViewById(R.id.container);
TextView tvTitle = (TextView) eventView.findViewById(R.id.textViewTitle);
TextView showLocation = (TextView)eventView.findViewById(R.id.At);
if (tvTitle.getParent() != null)
((ViewGroup) tvTitle.getParent()).removeView(tvTitle);
tvTitle.setText(title);
if (showLocation.getParent() != null)
((ViewGroup) showLocation.getParent()).removeView(showLocation);
showLocation.setText(location);
if(location == null)
{
showLocation.setVisibility(View.INVISIBLE);
}
else {
showLocation.setVisibility(View.VISIBLE);
}
int distance = (toMinutes - fromMinutes);
layoutParams.topMargin = dpToPixels(fromMinutes + 9);
layoutParams.height = dpToPixels(distance);
eventView.setLayoutParams(layoutParams);
dayplanView.addView(eventView);
dayplanView.addView(showLocation);
container.addView(tvTitle);
container.addView(showLocation);
eventView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(),AddEventActivity.class);
startActivity(i);
}
});
}
event view layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape"
android:layout_marginLeft="60dp"
android:id="@+id/container"
android:layout_marginRight="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/textViewTitle"
android:layout_marginLeft="10dp"
android:textColor="@android:color/white"
android:layout_marginTop="01dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/At"
android:layout_below="@+id/textViewTitle"
android:layout_toEndOf="@+id/textViewTitle"
android:layout_marginTop="01dp"
android:text="At : "
android:textColor="@android:color/white" />
</RelativeLayout>
</RelativeLayout>
Getting exception on container.addView(showLocation);
Same exception I had for title so I did this:
if (tvTitle.getParent() != null)
((ViewGroup) tvTitle.getParent()).removeView(tvTitle);
So the problem was solved. But now I want to ad another text view in the view. Its giving same error again I tried to remove this view same way.
if (showLocation.getParent() != null) ((ViewGroup) showLocation.getParent()).removeView(showLocation);
Still getting exception.
Whats wrong.?
Upvotes: 0
Views: 1100
Reputation: 2265
Generally IllegalStateException thorws when a method is invoked in an object which are not in proper state(Illegal State), For instance an object which is not properly initialized or stall state
Please have also a look in SOF What's the intended use of IllegalStateException? and https://softwareengineering.stackexchange.com/questions/246250/illegalstateexception-vs-illegalargumentexception
Upvotes: 1