我的应用程序具有以下特点。
- 在横向模式下,将屏幕分成两半并显示几个片段(“LocFragment”和“MapFragment”)。
- 在纵向模式下,在 ActionBar 中显示 2 个选项卡。一个 Fragment 覆盖整个屏幕,并在 Fragments 之间切换,带有 Tab 选择。
- 针对 4.x+,所以我不关心支持库或 ActionBarSherlock。
如果应用程序启动时手机处于横向模式,则一切正常。但是当 1) 手机处于纵向模式,2) 应用程序启动,以及 3) 手机转为横向模式时,应用程序会崩溃。
layour/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/FragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
layout-land/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="org.solamour.eq.LocFragment"
android:id="@+id/EarthquakeListFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment
android:name="org.solamour.eq.MapFragment"
android:id="@+id/EarthquakeMapFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
public class MainActivity extends Activity {
TabListener<LocFragment> locTabListener;
TabListener<MapFragment> mapTabListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
View fragmentContainer = findViewById(R.id.FragmentContainer);
boolean bLandscape = fragmentContainer == null;
if (!bLandscape) {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab listTab = actionBar.newTab();
locTabListener = new TabListener<LocFragment>
(this, R.id.FragmentContainer,
LocFragment.class);
listTab.setText("List")
.setContentDescription("List of locations")
.setTabListener(locTabListener);
actionBar.addTab(listTab);
Tab mapTab = actionBar.newTab();
mapTabListener = new TabListener<MapFragment>
(this, R.id.FragmentContainer,
MapFragment.class);
mapTab.setText("Map")
.setContentDescription("Map of interests")
.setTabListener(mapTabListener);
actionBar.addTab(mapTab);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
View fragmentContainer = findViewById(R.id.FragmentContainer);
boolean bLandscape = fragmentContainer == null;
if (!bLandscape) {
FragmentTransaction ft =
getFragmentManager().beginTransaction();
if (mapTabListener.fragment != null) {
ft.detach(mapTabListener.fragment);
}
if (locTabListener.fragment != null) {
ft.detach(locTabListener.fragment);
}
ft.commit();
}
super.onSaveInstanceState(outState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
View fragmentContainer = findViewById(R.id.FragmentContainer);
boolean bLandscape = fragmentContainer == null;
if (!bLandscape) {
locTabListener.fragment =getFragmentManager().
findFragmentByTag(LocFragment.class.getName());
mapTabListener.fragment = getFragmentManager().
findFragmentByTag(MapFragment.class.getName());
}
}
public static class TabListener<T extends Fragment>
implements ActionBar.TabListener {
private Fragment fragment;
private Activity activity;
private Class<T> fragmentClass;
private int fragmentContainer;
public TabListener(Activity activity,
int fragmentContainer, Class<T> fragmentClass) {
this.activity = activity;
this.fragmentContainer = fragmentContainer;
this.fragmentClass = fragmentClass;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (fragment == null) {
String fragmentName = fragmentClass.getName();
fragment = Fragment.instantiate(activity, fragmentName);
ft.add(fragmentContainer, fragment, fragmentName);
} else {
ft.attach(fragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
ft.detach(fragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
if (fragment != null)
ft.attach(fragment);
}
}
}
说到崩溃,Eclipse 只是说“无法启动活动 ComponentInfo{...}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment”。它必须在“setContentView(R.layout.activity_main)”中的某个地方,但我不确定如何设置源代码以在代码中遵循。我会很感激任何建议。谢谢你。
__
溶胶