Trt*_*Trt 0 android android-fragments
我有这个观点:
<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:background="@color/DeepSkyBlue"
tools:context=".MainPreviewActivity"
android:weightSum="5">
<fragment
android:name="com.apps.foo.bar.CleanPreviewFragment"
android:id="@+id/clean_preview_fragment"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"/>
<fragment
android:name="com.apps.foo.bar.ProcessedPreviewFragment"
android:id="@+id/processed_preview_fragment"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"/>
<View
android:id="@+id/center_dummy_view"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"/>
<ListView
android:layout_height="0dp"
android:layout_weight="2"
android:layout_width="match_parent"
android:id="@+id/lvImageProcessChoices"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我想要将带有id的片段clean_preview_fragment替换为带有id的片段processed_preview_fragment.
我的应用程序需要在后台运行一个摄像头,以便获取帧,处理它们并在其中显示它们processed_preview_fragment.
这是我的代码:
processedPreviewFragment = (ProcessedPreviewFragment) fragmentManager.findFragmentById(R.id.processed_preview_fragment);
if(getResources().getConfiguration().orientation == getResources().getConfiguration().ORIENTATION_PORTRAIT){
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.clean_preview_fragment, processedPreviewFragment);
fragmentTransaction.commit();
}
Run Code Online (Sandbox Code Playgroud)
那是我的MainActivity,onCreate.
我收到此错误:
java.lang.IllegalStateException: Can't change container ID of fragment ProcessedPreviewFragment{41b14770 #1 id=0x7f0e0050}: was 2131624016 now 2131624015
Run Code Online (Sandbox Code Playgroud)
您无法替换通过XML声明的片段.如果要在运行时将片段添加到容器,则应使用FragmentManager它来添加它.在这种情况下,容器应该是一些布局,例如FrameLayout.
因此,用<fragment>XML 替换您的标记,<FrameLayout>并CleanPreviewFragment在运行时添加.然后,当您需要更换它时,请FragmentTransaction像现在这样使用.你不应该再得到那个例外了.
| 归档时间: |
|
| 查看次数: |
983 次 |
| 最近记录: |