at android.view.ViewGroup.addViewInner(ViewGroup.java:3937)
at android.view.ViewGroup.addView(ViewGroup.java:3787)
at android.view.ViewGroup.addView(ViewGroup.java:3728)
at android.view.ViewGroup.addView(ViewGroup.java:3701)
at com.example.administrator.demo1.CustomActivity.initView(CustomActivity.java:65)
at com.example.administrator.demo1.CustomActivity.onCreate(CustomActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6049)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5298)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
自定义控件,向控件添加子控件时,会出现
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
这个bug,这是因为重复给某个控件设置爸爸,父控件。
例如图中,inflate ,此时,inflate是全局变量。所以当再次循环的时候,这个儿子还是这个儿子,再给这个儿子找个爸爸就乱了。
因此要重新再生个儿子2,再给这个儿子2找爸爸就不会出错。每次添加子控件的时候,一定要保证子控件没有被添加过父控件 如下图
切记,出现2次这种错误了~~@!!!!