Android方向更改导致应用程序崩溃
我有一个带有简单布局文件的活动,它将保存一个以编程方式创建并添加到其中的片段。该片段没有任何布局文件,因为我以编程方式创建布局,如下所示:Android方向更改导致应用程序崩溃,android,fragment,android-orientation,device-orientation,Android,Fragment,Android Orientation,Device Orientation,我有一个带有简单布局文件的活动,它将保存一个以编程方式创建并添加到其中的片段。该片段没有任何布局文件,因为我以编程方式创建布局,如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" andr
<?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>
我正在使用setRetainInstance(true)来保留fragmnet状态。一切似乎都很好,我的画作也很好,自我更新。但是,在方向更改时,我得到以下错误:
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)
从我读到目前为止,使用这样的充气机
View v = inflater.inflate(R.layout.camera_fragment, parent, false);
应该可以解决这个问题,但在我的例子中,我不使用充气机,而是动态地为片段创建布局
更新
这是我的舱单:
<?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>
非常感谢,您给了bubble一个父对象,然后尝试将其添加到父对象中。您可以(请纠正我这一点)使用
null
而不是充气机中的父项。充气
。谢谢Sebastian。气泡c-tor正在接收上下文。看一下上面的onCreateView,我根本没有使用拐点。我认为问题出在Bubble
类中。我只能想象Bubble
构造函数将新实例添加到父视图中。我们能看到代码吗?@dbnex14您是否尝试过(清理和)重建您的项目?我无法重现您遇到的错误。如果您的意思是ll.addView(bubble)
,那么是的,我知道了。我只是复制了你的代码并运行了它。除了默认值之外,您是否向清单中的
元素添加了其他属性?
<?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>
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;
}
}