我有一个活动和几个碎片。我必须在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 --- Manage of managed 异常堆栈跟踪---在java.lang.RuntimeException: java处的java.lang.reflect.InvocationTargetException 在引发的com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) by:java.lang.reflect.InvocationTargetException at at java处的java.lang.reflect.Method.invoke(Native Method) java处的java.lang.reflect.Method.invoke(Method.java:372) com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) at ... 1更多信息: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.TypeLoadException:无法加载类型'Android.OS.BaseBundle' 来自汇编'Mono.Android,Version = 0.0.0.0,Culture = neutral, 公钥= 84e04ff9cfb79065' 。在 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(包装器动态方法) object.e0a03ea8-44fe-4482-bf92-bc99edac0352(intptr,intptr)at at md5d7f0962782a324dbc9992a12c8322ca0.PagerSlidingTabStrip.n_onSaveInstanceState(母语 方法)在 md5d7f0962782a324dbc9992a12c8322ca0.PagerSlidingTabStrip.onSaveInstanceState(PagerSlidingTabStrip.java:86) 在android.view.View.dispatchSaveInstanceState(View.java:13550)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 $ 1.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 更
布局。
<?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>
答案 0 :(得分:0)
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");
}
答案 1 :(得分:0)
我不确定基于此片段,但如果您尝试访问前一个人提到的Android资源ID,则需要在Xamarin中使用Android.Resource.Id。
如果这不是您想要的,我会询问您是否在所需容器中有一个ID为content_frame的FrameLayout。
我设计了我的布局并包含了一个FrameLayout,这是我使用SupportFragmentManager切换的内容。