方向更改时未找到片段的视图

时间:2013-06-21 17:18:23

标签: android android-fragments xamarin.android xamarin

我在c#xamarin for Android中编写了一个应用程序。此代码是通用的,几乎可以在Android上运行。所以这个问题对Java和c#都有效。

所以我得到了这个应用程序,我的MainActivity是我的fragmentmanager。它正在取代这样的碎片:

private void SelectItem(int position)
    {

        currentFragment = null;
        Fragment findFrag = null;

        string tag = "";
        var arguments = new Bundle();
        switch(position) {
        case 0: // <-- Home Menu item

                tag = "home";

                findFrag = FragmentManager.FindFragmentByTag (tag);

                if (findFrag != null) {
                    currentFragment = findFrag;
                } else {
                    currentFragment = new HomeFragment ();  
                }


            break;
            case 1: // <-- Picture Menu item

                tag = "picture";

            findFrag = FragmentManager.FindFragmentByTag (tag);

                if (findFrag != null) {
                    currentFragment = findFrag;
                } else {
                    currentFragment = new PictureFragment ();
                }

            break;
            case 2: // <-- Album Menu item

                tag = "album";

            findFrag = FragmentManager.FindFragmentByTag (tag);

                if (findFrag != null) {
                    currentFragment = findFrag;
                } else {
                    currentFragment = new AlbumFragment ();
                }

            break;
            case 3: // <-- Settings Menu item

                tag = "settings";

            findFrag = FragmentManager.FindFragmentByTag (tag);

                if (findFrag != null) {
                    currentFragment = findFrag;
                } else {
                    currentFragment = new SettingsFragment ();
                }

            break;
            case 4: // <-- Logout Menu item
                System.Diagnostics.Debug.WriteLine ("You should be logging out");
            break;
        }

        FragmentManager.BeginTransaction()
            .Replace(Resource.Id.content_frame, currentFragment, tag)
            .AddToBackStack(null)               
                .Commit();



        _drawerList.SetItemChecked(position, true);
        ActionBar.Title = _title = _menuTitles[position];

        // Always close drawer when an item has been selected.
        _drawer.CloseDrawer (_drawerList);

    }

一切顺利,我可以在片段之间切换。但是当我改变方向时,它会崩溃,并显示以下错误消息:

No view found for id 0x7f080014 (PhotoWapp.PhotoWapp:id/content_frame) for fragment PictureFragment{b52cb3b0 #1 id=0x7f080014 picture}
[FragmentManager] Activity state:
[FragmentManager]   Local Activity b52f3060 State:
[FragmentManager]     mResumed=false mStopped=false mFinished=false
[FragmentManager]     mLoadersStarted=false
[FragmentManager]     mChangingConfigurations=false
[FragmentManager]     mCurrentConfig={1.0 310mcc260mnc en_US ldltr sw320dp w533dp h294dp 240dpi nrml long land finger qwerty/v/v tball/v s.7}
[FragmentManager]   Active Fragments in b52f3150:

在我的片段中,我设置了RetainInstance = true,但由于某种原因,它无法找到Resource.Id.content_frame的引用。我该怎么做才能防止这种情况发生?

引用Resource.Id.content_frame的布局是:Resource.Layout.Main。看起来像这样:

<?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">
<!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
<!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111" />
</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

我认为你应该使用Resource.Layout.nameofxmlresource而不是Resource.Id ......