使用替换时 FragmentManager 崩溃
FragmentManager crashes when using Replace
我有一个 Activity 和几个碎片。我必须替换 FrameLayout 中的片段。但是,当我使用此功能时,我的应用程序崩溃了。
这是函数的代码
Android.Support.V4.App.Fragment fragment = null;
switch (position) {
case 0:
fragment = new ScheduleFragment();
break;
case 1:
fragment = new MainFragment();
break;
}
if (fragment != null) {
Android.Support.V4.App.FragmentManager fragmentManager = SupportFragmentManager;
var transaction = fragmentManager.BeginTransaction();
transaction.Replace(Resource.Id.content_frame, fragment);
transaction.Commit();
mDrawerList.SetItemChecked (position, true);
SetTitle (mScreenTitles [position]);
mDrawerLayout.CloseDrawer (mDrawerList);
} else {
Log.Error (this.Class.Name, "Error, fragment not created");
}
这是一个错误:
Java.Lang.RuntimeException: java.lang.reflect.InvocationTargetException at --- End of managed
exception stack trace --- at java.lang.RuntimeException:
java.lang.reflect.InvocationTargetException at at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) at Caused
by: java.lang.reflect.InvocationTargetException at at
java.lang.reflect.Method.invoke(Native Method) at at
java.lang.reflect.Method.invoke(Method.java:372) at at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at ... 1 more at Caused by:
md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable:
System.TypeLoadException: Could not load type 'Android.OS.BaseBundle'
from assembly 'Mono.Android, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=84e04ff9cfb79065'. at
Android.Views.View.n_OnSaveInstanceState (intptr,intptr) [0x00009] in
/Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.View.cs:14822
at at (wrapper dynamic-method)
object.e0a03ea8-44fe-4482-bf92-bc99edac0352 (intptr,intptr) at at
md5d7f0962782a324dbc9992a12c8322ca0.PagerSlidingTabStrip.n_onSaveInstanceState(Native
Method) at at
md5d7f0962782a324dbc9992a12c8322ca0.PagerSlidingTabStrip.onSaveInstanceState(PagerSlidingTabStrip.java:86)
at at android.view.View.dispatchSaveInstanceState(View.java:13550) at
at
android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2859)
at at
android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at at android.view.View.saveHierarchyState(View.java:13533) at at
android.support.v4.app.FragmentManagerImpl.saveFragmentViewState(FragmentManager.java:1639)
at at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1023)
at at
android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1233)
at at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:696)
at at
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at at
android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:456)
at at android.os.Handler.handleCallback(Handler.java:739) at at
android.os.Handler.dispatchMessage(Handler.java:95) at at
android.os.Looper.loop(Looper.java:135) at at
android.app.ActivityThread.main(ActivityThread.java:5221) at ... 4
more
布局。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradientshape">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="УНИИ-ИТ, 1 курс, 11 - ПГ"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/info"
android:textColor="#ffffffff"
android:gravity="center_horizontal" />
<TextView
android:text="понедельник, 11 мая"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dayOfMonth"
android:textColor="#ffffffff"
android:gravity="center" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:fadingEdge="vertical" />
</LinearLayout>
<ListView
android:id="@+id/left_drawer"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:choiceMode="singleChoice"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111" />
</android.support.v4.widget.DrawerLayout>
我还为 viewPager 中的片段加载了片段布局。
在这里:
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LessonsView"
android:layout_marginLeft="5dp"
android:padding="15dp"
android:divider="@null"
android:dividerHeight="0dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp" />
以及 ViewPager 中 ListView 项目的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="1dp"
android:layout_marginTop="5dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:layout_marginTop="5dp"
android:layout_marginBottom="0.0dp"
android:background="@drawable/border">
<TextView
android:text=" 1"
android:id="@+id/PairNumber"
android:layout_width="35.5dp"
android:layout_height="wrap_content"
android:gravity="top"
android:background="@drawable/triangle"
android:alpha="0.8"
android:layout_gravity="left"
android:textColor="#ff000000"
android:textAlignment="center" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Инновационный менеджмент и развитие информационных технологий"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pairTitle"
android:layout_marginTop="7dp"
android:textColor="#ff000000" />
<TextView
android:text="Лекция"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pairType"
android:layout_marginTop="2dp"
android:textColor="#ff949494" />
</LinearLayout>
</LinearLayout>
<View
android:padding="5dp"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:background="#ffe5e8ea"
android:alpha="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:dividerHorizontal"
android:minWidth="45px"
android:minHeight="115px"
android:background="#ffffff"
android:paddingRight="2dp"
android:paddingLeft="2dp"
android:paddingTop="-1dp">
<ImageView
android:src="@drawable/icon_teacher"
android:layout_width="35.5dp"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:scaleType="center"
android:layout_weight="0"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:layout_marginTop="2dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:layout_gravity="center">
<TextView
android:text="Миленко"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Teacher"
android:textColor="#ff000000"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:background="#ffe5e8ea"
android:alpha="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:dividerHorizontal"
android:minWidth="45px"
android:minHeight="115px"
android:background="#ffffff"
android:paddingRight="2dp"
android:paddingLeft="2dp">
<ImageView
android:src="@drawable/icon_place"
android:layout_width="35.5dp"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:scaleType="center"
android:layout_weight="0"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:layout_marginTop="1dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center">
<TextView
android:text="Корпус:1, Аудитория 314"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pairPlace"
android:layout_gravity="center"
android:gravity="left"
android:textColor="#ff000000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4"
android:minWidth="45px"
android:minHeight="45px">
<TextView
android:text="20:15-21:45"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:id="@+id/pairTime"
android:layout_weight="3"
android:background="@drawable/timebackground"
android:gravity="center"
android:layout_gravity="top"
android:textColor="#ff000000" />
<Button
android:text="Напоминать"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:gravity="center"
android:id="@+id/NotifyBtn"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@drawable/buttonbackground"
android:textColor="#ffffffff"
android:layout_gravity="right"
android:drawableRight="@drawable/button_notify_inactive"
android:textSize="12dp"
android:alpha="0.9" />
</LinearLayout>
</LinearLayout>
Android.Support.V4.App.Fragment fragment = null;
switch (position) {
case 0:
fragment = new ScheduleFragment();
break;
case 1:
fragment = new MainFragment();
break;
}
if (fragment != null) {
Android.Support.V4.App.FragmentManager fragmentManager = SupportFragmentManager;
var transaction = fragmentManager.BeginTransaction();
// ---- change this line ---- //
transaction.Replace(R.id.content_frame, fragment);
transaction.Commit();
mDrawerList.SetItemChecked (position, true);
SetTitle (mScreenTitles [position]);
mDrawerLayout.CloseDrawer (mDrawerList);
} else {
Log.Error (this.Class.Name, "Error, fragment not created");
}
根据这个 snipit 我不确定,但是如果你试图访问一个 Android 资源 ID,比如前面提到的,你需要在 Xamarin 中使用 Android.Resource.Id。
如果这不是你想要的,我会问你是否在你想要的容器中有一个 FrameLayout,id 为 content_frame。
我设计了我的布局并包含一个 FrameLayout,这是我使用 SupportFragmentManager 切换进出的内容。
我有一个 Activity 和几个碎片。我必须替换 FrameLayout 中的片段。但是,当我使用此功能时,我的应用程序崩溃了。 这是函数的代码
Android.Support.V4.App.Fragment fragment = null;
switch (position) {
case 0:
fragment = new ScheduleFragment();
break;
case 1:
fragment = new MainFragment();
break;
}
if (fragment != null) {
Android.Support.V4.App.FragmentManager fragmentManager = SupportFragmentManager;
var transaction = fragmentManager.BeginTransaction();
transaction.Replace(Resource.Id.content_frame, fragment);
transaction.Commit();
mDrawerList.SetItemChecked (position, true);
SetTitle (mScreenTitles [position]);
mDrawerLayout.CloseDrawer (mDrawerList);
} else {
Log.Error (this.Class.Name, "Error, fragment not created");
}
这是一个错误:
Java.Lang.RuntimeException: java.lang.reflect.InvocationTargetException at --- End of managed exception stack trace --- at java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) at Caused by: java.lang.reflect.InvocationTargetException at at java.lang.reflect.Method.invoke(Native Method) at at java.lang.reflect.Method.invoke(Method.java:372) at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at ... 1 more at Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.TypeLoadException: Could not load type 'Android.OS.BaseBundle' from assembly 'Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065'. at Android.Views.View.n_OnSaveInstanceState (intptr,intptr) [0x00009] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.View.cs:14822 at at (wrapper dynamic-method) object.e0a03ea8-44fe-4482-bf92-bc99edac0352 (intptr,intptr) at at md5d7f0962782a324dbc9992a12c8322ca0.PagerSlidingTabStrip.n_onSaveInstanceState(Native Method) at at md5d7f0962782a324dbc9992a12c8322ca0.PagerSlidingTabStrip.onSaveInstanceState(PagerSlidingTabStrip.java:86) at at android.view.View.dispatchSaveInstanceState(View.java:13550) at at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2859) at at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865) at at android.view.View.saveHierarchyState(View.java:13533) at at android.support.v4.app.FragmentManagerImpl.saveFragmentViewState(FragmentManager.java:1639) at at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1023) at at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1233) at at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:696) at at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499) at at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:456) at at android.os.Handler.handleCallback(Handler.java:739) at at android.os.Handler.dispatchMessage(Handler.java:95) at at android.os.Looper.loop(Looper.java:135) at at android.app.ActivityThread.main(ActivityThread.java:5221) at ... 4 more
布局。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradientshape">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="УНИИ-ИТ, 1 курс, 11 - ПГ"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/info"
android:textColor="#ffffffff"
android:gravity="center_horizontal" />
<TextView
android:text="понедельник, 11 мая"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dayOfMonth"
android:textColor="#ffffffff"
android:gravity="center" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:fadingEdge="vertical" />
</LinearLayout>
<ListView
android:id="@+id/left_drawer"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:choiceMode="singleChoice"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111" />
</android.support.v4.widget.DrawerLayout>
我还为 viewPager 中的片段加载了片段布局。 在这里:
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LessonsView"
android:layout_marginLeft="5dp"
android:padding="15dp"
android:divider="@null"
android:dividerHeight="0dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp" />
以及 ViewPager 中 ListView 项目的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="1dp"
android:layout_marginTop="5dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:layout_marginTop="5dp"
android:layout_marginBottom="0.0dp"
android:background="@drawable/border">
<TextView
android:text=" 1"
android:id="@+id/PairNumber"
android:layout_width="35.5dp"
android:layout_height="wrap_content"
android:gravity="top"
android:background="@drawable/triangle"
android:alpha="0.8"
android:layout_gravity="left"
android:textColor="#ff000000"
android:textAlignment="center" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Инновационный менеджмент и развитие информационных технологий"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pairTitle"
android:layout_marginTop="7dp"
android:textColor="#ff000000" />
<TextView
android:text="Лекция"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pairType"
android:layout_marginTop="2dp"
android:textColor="#ff949494" />
</LinearLayout>
</LinearLayout>
<View
android:padding="5dp"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:background="#ffe5e8ea"
android:alpha="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:dividerHorizontal"
android:minWidth="45px"
android:minHeight="115px"
android:background="#ffffff"
android:paddingRight="2dp"
android:paddingLeft="2dp"
android:paddingTop="-1dp">
<ImageView
android:src="@drawable/icon_teacher"
android:layout_width="35.5dp"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:scaleType="center"
android:layout_weight="0"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:layout_marginTop="2dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:layout_gravity="center">
<TextView
android:text="Миленко"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Teacher"
android:textColor="#ff000000"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:background="#ffe5e8ea"
android:alpha="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:dividerHorizontal"
android:minWidth="45px"
android:minHeight="115px"
android:background="#ffffff"
android:paddingRight="2dp"
android:paddingLeft="2dp">
<ImageView
android:src="@drawable/icon_place"
android:layout_width="35.5dp"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:scaleType="center"
android:layout_weight="0"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:layout_marginTop="1dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center">
<TextView
android:text="Корпус:1, Аудитория 314"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pairPlace"
android:layout_gravity="center"
android:gravity="left"
android:textColor="#ff000000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4"
android:minWidth="45px"
android:minHeight="45px">
<TextView
android:text="20:15-21:45"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:id="@+id/pairTime"
android:layout_weight="3"
android:background="@drawable/timebackground"
android:gravity="center"
android:layout_gravity="top"
android:textColor="#ff000000" />
<Button
android:text="Напоминать"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:gravity="center"
android:id="@+id/NotifyBtn"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@drawable/buttonbackground"
android:textColor="#ffffffff"
android:layout_gravity="right"
android:drawableRight="@drawable/button_notify_inactive"
android:textSize="12dp"
android:alpha="0.9" />
</LinearLayout>
</LinearLayout>
Android.Support.V4.App.Fragment fragment = null;
switch (position) {
case 0:
fragment = new ScheduleFragment();
break;
case 1:
fragment = new MainFragment();
break;
}
if (fragment != null) {
Android.Support.V4.App.FragmentManager fragmentManager = SupportFragmentManager;
var transaction = fragmentManager.BeginTransaction();
// ---- change this line ---- //
transaction.Replace(R.id.content_frame, fragment);
transaction.Commit();
mDrawerList.SetItemChecked (position, true);
SetTitle (mScreenTitles [position]);
mDrawerLayout.CloseDrawer (mDrawerList);
} else {
Log.Error (this.Class.Name, "Error, fragment not created");
}
根据这个 snipit 我不确定,但是如果你试图访问一个 Android 资源 ID,比如前面提到的,你需要在 Xamarin 中使用 Android.Resource.Id。
如果这不是你想要的,我会问你是否在你想要的容器中有一个 FrameLayout,id 为 content_frame。
我设计了我的布局并包含一个 FrameLayout,这是我使用 SupportFragmentManager 切换进出的内容。