0

I have MyCompassView class that extends view and it draws a compass,SurfaceHolder activity that opens the camera on the surface holder and finally,MainActivity that should be overlayed by the aforementioned classes.The way I use to overlay SurfaceHolder activity on the the MainActivity is as follows

myCameraSurfaceHolder = new SurfaceHolderActivity(this); setContentView(myCameraSurfaceHolder); and it worked find and the camera works fine on the surface holder.

The problem Is when I try to overlay the surface of the MainActivity with the view of MyCompassView. To achieve that I used:

loiViewInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); loiViewInflater = LayoutInflater.from(getApplicationContext()); compassOverLayView = loiViewInflater.inflate(R.layout.activity_viewactivity, null); addContentView(compassOverLayView,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

But the addContentView(....) is placed within the onSensorChanged of the sensor listener because with every reading from the sensor i want to draw the compass with different pointer orientation based on the azimuth value. And logcat complains and generates the below messages. please help me to correct the error.

Logcat_OutPut:

04-26 04:07:21.295: E/AndroidRuntime(26921): FATAL EXCEPTION: main
04-26 04:07:21.295: E/AndroidRuntime(26921): java.lang.IllegalStateException: The  
specified child already has a parent. You must call removeView() on the child's parent  
first.
04-26 04:07:21.295: E/AndroidRuntime(26921):    at  
android.view.ViewGroup.addViewInner(ViewGroup.java:3739)
04-26 04:07:21.295: E/AndroidRuntime(26921):    at  
android.view.ViewGroup.addView(ViewGroup.java:3610)
04-26 04:07:21.295: E/AndroidRuntime(26921):    at  
android.view.ViewGroup.addView(ViewGroup.java:3586)
04-26 04:07:21.295: E/AndroidRuntime(26921):    at   
com.android.internal.policy.impl.PhoneWindow.addContentView(PhoneWindow.java:392)
04-26 04:07:21.295: E/AndroidRuntime(26921):    at   
android.app.Activity.addContentView(Activity.java:2004)
04-26 04:07:21.295: E/AndroidRuntime(26921):    at  

android.support.v7.app.ActionBarActivity.superAddContentView(ActionBarActivity.java:228)

1 Answers1

0

as the error log says: "The specified child already has a parent. You must call removeView() on the child's parent first".

That error refers to the compassOverLayView which was already added to a parent view and cannot be added again.

This is not a very good method of refreshing a UI element, addContentView wasn't meant to be called repeatedly.

If you've created a custom view to display your compass, try adding a refresh method to it, and simply call it.

marmor
  • 27,641
  • 11
  • 107
  • 150