1
我正在爲c#xamarin編寫應用程序。此代碼具有普遍性,應該幾乎適用於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>
你不知道我在說什麼。 –
對不起,上一篇文章。我沒有仔細閱讀你的問題。一個問題,你是否在你的活動加載時添加一個起始片段?替換方法的文檔說:替換已添加到容器的現有片段。這與爲所有當前添加的片段調用remove(Fragment)相同的containerViewId,然後使用此處給出的相同參數添加(int,Fragment,String)基本相同。如果您的活動重新創建時沒有添加碎片,則您的FrameLayout將不會替換碎片 – Emmanuel