我的应用具有以下特征。
如果应用程序启动时手机处于横向模式,一切正常。但是当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:二进制XML文件行#7:错误膨胀类片段”。它必须位于“setContentView(R.layout.activity_main)”中,但我不知道如何设置源代码以遵循代码。我很感激任何建议。谢谢。
__
溶胶
答案 0 :(得分:0)
错误位于横向布局的第7行:
fragment
android:name="org.solamour.eq.LocFragment"
也许这个类的路径是错误的,或者LocFragment可能有错误,当你显示它时会发生崩溃。