我有一个管理片段列表的活动。我添加的一个新片段是扩展 ListFragmet 而不是 Fragment,我将它作为其他片段添加到活动 xml 布局文件中。我在声明 ListFragment 的行的这个文件中得到了一个 InflatingException。这个声明有什么问题?
活动 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.foodexp01b.HomeFragment"
android:id="@+id/homeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:name="com.example.foodexp01b.LoginFragment"
android:id="@+id/loginFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:name="com.example.foodexp01b.SettingsFragment"
android:id="@+id/settingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:name="com.example.foodexp01b.DestinationFragment"
android:id="@+id/destinationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/mapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<fragment android:name="com.example.foodexp01b.RestaurantFragment"
android:id="@+id/restaurantFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:name="com.example.foodexp01b.FavoritesFragment"
android:id="@+id/favoritesFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Activity OnCreate 代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
fragments[LOGIN] = fm.findFragmentById(R.id.loginFragment);
fragments[HOME] = fm.findFragmentById(R.id.homeFragment);
fragments[SETTINGS] = fm.findFragmentById(R.id.settingsFragment);
fragments[SETTINGS].getView().findViewById(R.id.back_button1)
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
currentFragment = HOME;
showFragment(HOME, false);
}
});
fragments[DESTINATIONS] = fm.findFragmentById(R.id.destinationFragment);
fragments[DESTINATIONS].getView().findViewById(R.id.back_button2)
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
currentFragment = HOME;
showFragment(HOME, false);
}
});
fragments[MAP] = fm.findFragmentById(R.id.mapFragment);
fragments[RESTAURANT] = fm.findFragmentById(R.id.restaurantFragment);
fragments[RESTAURANT].getView().findViewById(R.id.restaurant_back_button)
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
currentFragment = MAP;
showFragment(MAP, false);
}
});
fragments[FAVORITES].getView().findViewById(R.id.favoritesFragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
}
收藏夹Fragment OnCreate查看代码
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(android.R.layout.simple_list_item_1, container, false);
favorites = new ArrayList<String>();
setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, favorites));
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
}
});
return view;
}
日志
06-30 20:17:53.609: E/AndroidRuntime(675): FATAL EXCEPTION: main
06-30 20:17:53.609: E/AndroidRuntime(675): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.foodexp01b/com.example.foodexp01b.MainActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
06-30 20:17:53.609: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.app.ActivityThread.access$600(ActivityThread.java:134)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.os.Looper.loop(Looper.java:137)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.app.ActivityThread.main(ActivityThread.java:4744)
06-30 20:17:53.609: E/AndroidRuntime(675): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 20:17:53.609: E/AndroidRuntime(675): at java.lang.reflect.Method.invoke(Method.java:511)
06-30 20:17:53.609: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-30 20:17:53.609: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-30 20:17:53.609: E/AndroidRuntime(675): at dalvik.system.NativeStart.main(Native Method)
06-30 20:17:53.609: E/AndroidRuntime(675): Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
06-30 20:17:53.609: E/AndroidRuntime(675): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-30 20:17:53.609: E/AndroidRuntime(675): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:262)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.app.Activity.setContentView(Activity.java:1867)
06-30 20:17:53.609: E/AndroidRuntime(675): at com.example.foodexp01b.MainActivity.onCreate(MainActivity.java:118)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.app.Activity.performCreate(Activity.java:5008)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)
06-30 20:17:53.609: E/AndroidRuntime(675): ... 11 more
06-30 20:17:53.609: E/AndroidRuntime(675): Caused by: java.lang.IllegalStateException: Content view not yet created
06-30 20:17:53.609: E/AndroidRuntime(675): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
06-30 20:17:53.609: E/AndroidRuntime(675): at com.example.foodexp01b.FavoritesFragment.onCreateView(FavoritesFragment.java:63)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
06-30 20:17:53.609: E/AndroidRuntime(675): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
06-30 20:17:53.609: E/AndroidRuntime(675): ... 21 more