【问题标题】:Android - Error inflecting class fragmentAndroid - 影响类片段的错误
【发布时间】:2015-02-16 11:10:08
【问题描述】:

我有两个片段和一个具有导航抽屉的主要活动。在导航抽屉中,我有两个菜单项,即 Home 和 Places。我为这两个菜单项创建了 xml 片段。

fragment_home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
tools:context="com.gaurav.googlemap.HomeMap" >

<fragment
    class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</fragment>

</RelativeLayout>

fragment_places.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Places"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Java 代码

package com.beproject.ourway;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


public class MainHome extends FragmentActivity{
    private String[] mNavigationDrawerItemTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    Fragment fragment = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_home);

    // Initialization

    mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);


    // Defining drawer items
    ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[2];

    drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_launcher, "Home");
    drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_launcher, "Places");


    DrawerItemCustomAdapter adapter = 
            new DrawerItemCustomAdapter(this, R.layout.home_drawer_item, drawerItem);

    mDrawerList.setAdapter(adapter);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // selectItem(0);
}

public class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }

}

void selectItem(int position) {



    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new PlacesFragment();
        break;

    default:
        break;
    }

    if (fragment != null) {

        mDrawerLayout.closeDrawer(mDrawerList);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        getActionBar().setTitle(mNavigationDrawerItemTitles[position]);

    } else {
        Log.e("MainActivity", "Error in creating fragment");
    }
}

public class HomeFragment extends Fragment {

    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        return rootView;
    }

}

public class PlacesFragment extends Fragment {

    public PlacesFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_places, container, false);

        return rootView;
    }

}
}

问题

当我运行项目时,我点击主页菜单,然后它会以很好的方式向我显示带有地图的主页片段。之后我点击地方然后它显示我的地方片段很好。 但是当我再次点击主页时,我的应用程序将因以下异常而崩溃

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gaurav.googlemap/com.gaurav.googlemap.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment

java 文件中是否有任何更正以正确打开片段?
谢谢

LogCat

02-16 16:50:58.324: V/Provider/Setting(25249): from settings cache , name = sound_effects_enabled value = 0
02-16 16:50:58.358: D/AndroidRuntime(25249): Shutting down VM
02-16 16:50:58.359: W/dalvikvm(25249): threadid=1: thread exiting with uncaught exception (group=0x4188b908)
02-16 16:50:58.388: E/AndroidRuntime(25249): FATAL EXCEPTION: main
02-16 16:50:58.388: E/AndroidRuntime(25249): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-16 16:50:58.388: E/AndroidRuntime(25249):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-16 16:50:58.388: E/AndroidRuntime(25249):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-16 16:50:58.388: E/AndroidRuntime(25249):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)

【问题讨论】:

  • 请发布您的完整 LogCat
  • 我认为这是因为您使用 support.v4.app.FragmentActivity 和 android.app.Fragment,您应该使用 support.v4.app.Fragment
  • 在哪里更改我的代码? @SboneloMbhamali
  • @93gaurav93:在HomeFragment 中扩展SupportMapFragment 而不是Fragment
  • 删除这一行:import android.app.Fragment;并将其替换为 import android.support.v4.app.Fragment;

标签: java android xml android-layout android-fragments


【解决方案1】:

当我们经历片段生命周期时,似乎片段再次从 onDestroyView() 调用时,无法调用 onCreateView()。所以我的建议是通过方法保存片段的实例

addToBackStack()

或使用

设置保留状态(真)

【讨论】:

  • 在哪里以及如何将此方法放入我的代码中?我不知道这个
  • 我已经修改了我的代码,但这不起作用... :(
【解决方案2】:

如果您拥有实现googleMap 所需的所有权限,请检查您的mainfaist 文件,请查看here。 如果所有权限都正常,请尝试更改您的代码,如下所示:

因为您在 selectItem 方法中使用 SupportMapFragment 并且您的变量 Fragment 包含 HomeFragment 类(其中是地图),所以您应该导入这个:

import android.support.v4.app.Fragment;

还有这一行:

 FragmentManager fragmentManager = getFragmentManager();

你应该改成:

FragmentManager fragmentManager = getSupportFragmentManager();

并导入:

import android.support.v4.app.FragmentManager;

原因与我上面提到的相同。

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多