Question

I'm trying to remove an inflated view. This is how I inflate the view:

ViewGroup vg = (ViewGroup)findViewById(android.R.id.content).getRootView();
mOverflowMenuView = View.inflate(this, R.layout.overflow_menu, vg);     

And this is how I try to remove it from onBackPressed

ViewGroup vg = (ViewGroup)(mOverflowMenuView.getParent());
vg.removeView(mOverflowMenuView);

But I get a class cast exception:

03-11 22:47:31.848: E/AndroidRuntime(26357): java.lang.ClassCastException: android.view.ViewRootImpl cannot be cast to android.view.ViewGroup

Any ideas?

Was it helpful?

Solution 2

Finally solved it. It seems like my View were added to a RelativeLayout under the root. To get that RelativeLayout handle I had to do:

ViewGroup vg = (ViewGroup)findViewById(R.id.board_root_view).getRootView();     
RelativeLayout v = (RelativeLayout)mOverflowListView.getParent();
vg.removeView(v);       

OTHER TIPS

If you are getting the classcastexception on the below line:

  ViewGroup vg = (ViewGroup)findViewById(android.R.id.content).getRootView();

Then I would suggest that you place an id in the root view of your layout and then inflate it using the findViewById() directly and then use the removeView method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top