FragmentManager crashes when using Replace

五迷三道 提交于 2019-12-23 21:46:01

问题


I have an Activity and few Fragments. I have to replace fragment in FrameLayout. But when I'm using this function my app crashes. Here is the code of function

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");
        }

And here is an error:

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$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 more

Layout.

<?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>

Also i load the fragment layout for fragments in viewPager. Here is it:

<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" />

And layout of ListView items in ViewPager.

<?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>

回答1:


I am not certain based on this snipit, but if you are trying to access an Android resource Id such as mentioned by the previous person, you need to use Android.Resource.Id in Xamarin.

If this is not what you want, I would ask if you have a FrameLayout inside your desired container with the id content_frame.

I designed my layout and include a FrameLayout which is what I switch in and out of using the SupportFragmentManager.




回答2:


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");
    }


来源:https://stackoverflow.com/questions/30219007/fragmentmanager-crashes-when-using-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!