我有MyCompassView
一个扩展视图的类,它绘制一个指南针,SurfaceHolder
在表面支架上打开相机的活动,最后,MainActivity
应该由上述类覆盖。我用来覆盖SurfaceHolder
活动的MainActivity
方式如下
myCameraSurfaceHolder = new SurfaceHolderActivity(this);
setContentView(myCameraSurfaceHolder);
它工作正常,相机在表面支架上工作正常。
问题是当我尝试MainActivity
用MyCompassView
. 为了实现这一点,我使用了:
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));
但是它addContentView(....)
被放置在onSensorChanged
传感器侦听器的内部,因为每次从传感器读取时,我都想根据方位角值绘制具有不同指针方向的指南针。并且 logcat 抱怨并生成以下消息。请帮我纠正错误。
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)