问题
I have an activity with simple layout file that will hold a fragment created and added to it programmatically. The fragment does not have any layout file since I create layout programmatically as shown below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/bubble_fragment">
</LinearLayout>
My Fragment's onCreateView() is like this and it will create layout and class Bubble which does some drawing on Canvas using Handler (android.os.Handler):
public class Bubble extends View implements View.OnClickListener {
private static final boolean BUBBLING = true; //thread is running to draw
private Paint paint;
private ShapeDrawable bubble;
// coordiantes, radius etc
private int x;
private int y;
private int r;
private int w = 200;
private int h = 200;
//handler to invalidate (force redraw on main GUI thread from this thread)
private Handler handler = new Handler();
public Bubble(Context context) {
super(context);
x = w/2;
y = h/2;
r = w/2;
bubble = new ShapeDrawable(new OvalShape());
bubble.getPaint().setColor(Color.BLUE);
bubble.setBounds(0, 0, r, r);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(0);
}
@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh){
//set bubble parameters (center, size, etc)
startAnimation();
}
public void startAnimation(){
new Thread(new Runnable() {
public void run() {
while (BUBBLING) {
moveBubble();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
//update by invalidating on main UI thread
handler.post(new Runnable() {
public void run() {
invalidate();
}
});
}
}
}).start();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// draws bubble on canvas
}
private void moveBubble(){
x = x + 1;
y = y + 1;
}
@Override
public void onClick(View v) {
Toast.makeText(this.getContext(), "Tapped on screen", Toast.LENGTH_LONG).show();
}
}
I am using setRetainInstance(true) to retain my fragmnet state. Everything seems to be working fine and my drawing works and refreshes itself just fine. However, on orientation change, I get this error:
E/AndroidRuntime: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3936)
at android.view.ViewGroup.addView(ViewGroup.java:3786)
at android.view.ViewGroup.addView(ViewGroup.java:3727)
at android.view.ViewGroup.addView(ViewGroup.java:3700)
at my.study.android.bubble.BubbleFragment.onCreateView(BubbleFragment.java:52)
From what I have read so far, using inflator like this
View v = inflater.inflate(R.layout.camera_fragment, parent, false);
should solve this problem but in my case, I do not use inflater, I create layout for my fragment dynamically.
UPDATE Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.study.android.bubble" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
>
<!-- removed this from above to show ActionBar
android:theme="@style/AppTheme.NoActionBar"> -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
UPDATE 2: I figure out why it was crashing. I was creating Bubble inside onCreate(). If I move it to onCreateView(), then on orientation, it will not crash. However, my animation is not retained on orientation, meaning the bubble is draws at its initial location when I change orientation:
public class BubbleFragment extends Fragment {
Bubble bubble;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//retain fragment
setRetainInstance(true);
//bubble = new Bubble(getActivity()); //THIS WILL CRASH APP, MOVE TO onCreateView instetad
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
100);
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
// instantiate my class that does drawing on Canvas
bubble = new Bubble(getActivity());
bubble.setLayoutParams(lp);
bubble.setBackgroundColor(Color.BLUE);
ll.addView(bubble); //if you create bubble in onCreate() this will crash. Create bubble in onCreateView
return ll;
}
}
Much appreciated,
来源:https://stackoverflow.com/questions/33113562/android-orientation-change-crashes-application