1

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

所以我得到了这个应用程序,我的 MainActivity 是我的片段管理器。它正在替换这样的片段:

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

1 回答 1

0

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

于 2013-06-21T17:36:02.690 回答