Java 二进制XML文件:膨胀类片段时出错

Java 二进制XML文件:膨胀类片段时出错,java,android,android-layout,android-fragments,navigation-drawer,Java,Android,Android Layout,Android Fragments,Navigation Drawer,我知道这是一个常见的问题,但我已经尝试了网络上所有的解决方案,但它仍然不起作用,所以请帮助 我正在尝试在我的项目中创建一个导航抽屉。在第一次运行时,我遇到了logcat错误,即“”android.view.InflateException:Binary XML file:error inflating class fragment” 我已经创建了4个文件: 主要活动 其布局 抽屉活动 其布局 代码如下: 1) 我称之为家的主要活动 package GJ.helpinghand; import

我知道这是一个常见的问题,但我已经尝试了网络上所有的解决方案,但它仍然不起作用,所以请帮助

我正在尝试在我的项目中创建一个导航抽屉。在第一次运行时,我遇到了logcat错误,即“
”android.view.InflateException:Binary XML file:error inflating class fragment”

我已经创建了4个文件:

  • 主要活动
  • 其布局
  • 抽屉活动
  • 其布局
  • 代码如下:

    1) 我称之为家的主要活动

    package GJ.helpinghand;
    
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.widget.DrawerLayout;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    
    public class Home extends FragmentActivity {
    
    private ListView con;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    
    
        Drawer drawer_instance = (Drawer) getFragmentManager().findFragmentById(R.id.drawer_frag);
    
        drawer_instance.setUp(R.id.drawer_frag,(DrawerLayout) findViewById(R.id.drawer_layout)); 
    
        /*Drawer_try drawer_instance = (Drawer_try) getFragmentManager().findFragmentById(R.id.drawer_frag);
    
        drawer_instance.setUp(R.id.drawer_frag,(DrawerLayout) findViewById(R.id.drawer_layout));
        */
        con = (ListView)findViewById(R.id.content);
    
        final String[] info = new String[] { "Android List View", "Adapter implementation" };
    
        ArrayAdapter<String> adap = new ArrayAdapter<String>(getApplicationContext(),R.layout.listlay,R.id.title, info);
        con.setAdapter(adap);
    
        con.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                String to = info[position];
                Toast.makeText(getApplicationContext(), to , Toast.LENGTH_LONG).show();
            }
    
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
       }
    
    GJ.helpinghand包;
    导入android.os.Bundle;
    导入android.support.v4.app.FragmentActivity;
    导入android.support.v4.widget.DrawerLayout;
    导入android.view.Menu;
    导入android.view.MenuItem;
    导入android.view.view;
    导入android.widget.AdapterView;
    导入android.widget.ArrayAdapter;
    导入android.widget.ListView;
    导入android.widget.Toast;
    公共类家庭扩展了碎片化活动{
    私有列表视图;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    抽屉实例=(抽屉)getFragmentManager().findFragmentById(R.id.Drawer\u frag);
    抽屉实例设置(R.id.drawer\u frag,(抽屉布局)findViewById(R.id.drawer\u布局));
    /*Drawer\u try Drawer\u实例=(Drawer\u try)getFragmentManager().findframentbyid(R.id.Drawer\u frag);
    抽屉实例设置(R.id.drawer\u frag,(抽屉布局)findViewById(R.id.drawer\u布局));
    */
    con=(ListView)findViewById(R.id.content);
    最终字符串[]信息=新字符串[]{“Android列表视图”,“适配器实现”};
    ArrayAdapter adap=新的ArrayAdapter(getApplicationContext(),R.layout.listlay,R.id.title,info);
    con.setAdapter(adap);
    con.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
    @凌驾
    public void onItemClick(AdapterView父级、视图、,
    内部位置,长id){
    //TODO自动生成的方法存根
    字符串to=信息[位置];
    Toast.makeText(getApplicationContext(),to,Toast.LENGTH_LONG.show();
    }
    });
    }
    @凌驾
    公共布尔onCreateOptions菜单(菜单){
    //为菜单充气;这会将项目添加到操作栏(如果存在)。
    getMenuInflater().充气(R.menu.home,menu);
    返回true;
    }
    @凌驾
    公共布尔值onOptionsItemSelected(菜单项项){
    //处理操作栏项目单击此处。操作栏将
    //自动处理Home/Up按钮上的点击,只要
    //在AndroidManifest.xml中指定父活动时。
    int id=item.getItemId();
    if(id==R.id.action\u设置){
    返回true;
    }
    返回super.onOptionsItemSelected(项目);
    }
    }
    
  • 导致错误的主活动的XML:

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="GJ.helpinghand.Home" >
    
    <RelativeLayout 
        android:id="@+id/core"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView1"
            android:background="@android:color/darker_gray"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="@string/file"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <ListView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/textView1"
            android:divider="@android:color/holo_orange_light" />
    
    </RelativeLayout>
    
     <fragment 
        android:id="@+id/drawer_frag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        class="com.fragment.NavigationDrawerFragment"
        tools:layout="@layout/fragment_drawer" />
    
    
    

  • 3) 抽屉的java文件:

    package GJ.helpinghand;
    
    import android.app.ActionBar;
    import android.app.Activity;
    import android.app.Fragment;
    import android.content.SharedPreferences;
    import android.content.res.Configuration;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.support.v4.app.ActionBarDrawerToggle;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    
    public class Drawer extends Fragment {
    
    
    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
    private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
    
    private NavigationDrawerCallbacks mCallbacks;
    private ActionBarDrawerToggle mDrawerToggle;
    
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerListView;
    private View mFragmentContainerView;
    
    private int mCurrentSelectedPosition = 0;
    private boolean mFromSavedInstanceState;
    private boolean mUserLearnedDrawer;
    
    public Drawer() {
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(getActivity());
        mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
    
        if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState
                    .getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }
    
        // Select either the default item (0) or the last selected item.
        selectItem(mCurrentSelectedPosition);
    
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Indicate that this fragment would like to influence the set of
        // actions in the action bar.
        setHasOptionsMenu(true);
    }
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_drawer, container, false);
        mDrawerListView
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        selectItem(position);
                    }
                });
        mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar()
                .getThemedContext(), android.R.layout.simple_list_item_1,
                android.R.id.text1, new String[] {
                        getString(R.string.title_section1),
                        getString(R.string.title_section2),
                        getString(R.string.title_section3), }));
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
    }
    
    
    public boolean isDrawerOpen() {
        return mDrawerLayout != null
                && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
    }
    
    
    public void setUp(int fragmentId, DrawerLayout drawerLayout) {
        mFragmentContainerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;
    
        // set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);
    
        // set up the drawer's list view with items and click listener
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
    
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,R.drawable.ic_drawer,
        R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
    
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (!isAdded()) {
                    return;
                }
    
                getActivity().invalidateOptionsMenu(); // calls
                                                                // onPrepareOptionsMenu()
            }
    
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!isAdded()) {
                    return;
                }
    
                if (!mUserLearnedDrawer) {
                    // The user manually opened the drawer; store this flag to
                    // prevent auto-showing
                    // the navigation drawer automatically in the future.
                    mUserLearnedDrawer = true;
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(getActivity());
                    sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true)
                            .commit();
                }
    
                getActivity().invalidateOptionsMenu(); // calls
                                                                // onPrepareOptionsMenu()
            }
        };
    
        // If the user hasn't 'learned' about the drawer, open it to introduce
        // them to the drawer,
        // per the navigation drawer design guidelines.
        if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
            mDrawerLayout.openDrawer(mFragmentContainerView);
        }
    
        // Defer code dependent on restoration of previous instance state.
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });
    
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }
    
    private void selectItem(int position) {
        mCurrentSelectedPosition = position;
        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallbacks = (NavigationDrawerCallbacks) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(
                    "Activity must implement NavigationDrawerCallbacks.");
        }
    }
    
    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Forward the new configuration the drawer toggle component.
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // If the drawer is open, show the global app actions in the action bar.
        // See also
        // showGlobalContextActionBar, which controls the top-left area of the
        // action bar.
        if (mDrawerLayout != null && isDrawerOpen()) {
            inflater.inflate(R.menu.global, menu);
            showGlobalContextActionBar();
        }
        super.onCreateOptionsMenu(menu, inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
    
        if (item.getItemId() == R.id.action_example) {
            Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT)
                    .show();
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    /**
     * Per the navigation drawer design guidelines, updates the action bar to
     * show the global app 'context', rather than just what's in the current
     * screen.
     */
    
    private void showGlobalContextActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setTitle(R.string.app_name);
    }
    
    private ActionBar getActionBar() {
        return  getActivity().getActionBar();
    }
    
    /**
     * Callbacks interface that all activities using this fragment must
     * implement.
     */
    public static interface NavigationDrawerCallbacks {
        /**
         * Called when an item in the navigation drawer is selected.
         */
        void onNavigationDrawerItemSelected(int position);
    }
    }
    
    GJ.helpinghand包;
    导入android.app.ActionBar;
    导入android.app.Activity;
    导入android.app.Fragment;
    导入android.content.SharedReferences;
    导入android.content.res.Configuration;
    导入android.os.Bundle;
    导入android.preference.PreferenceManager;
    导入android.support.v4.app.ActionBarDrawerToggle;
    导入android.support.v4.view.GravityCompat;
    导入android.support.v4.widget.DrawerLayout;
    导入android.view.LayoutInflater;
    导入android.view.Menu;
    导入android.view.MenuInflater;
    导入android.view.MenuItem;
    导入android.view.view;
    导入android.view.ViewGroup;
    导入android.widget.AdapterView;
    导入android.widget.ArrayAdapter;
    导入android.widget.ListView;
    导入android.widget.Toast;
    公共类抽屉扩展片段{
    私有静态最终字符串状态\u SELECTED\u POSITION=“SELECTED\u navigation\u drawer\u POSITION”;
    私有静态最终字符串PREF\u USER\u LEARNED\u DRAWER=“导航\u DRAWER\u LEARNED”;
    专用导航抽屉回调mCallbacks;
    私有操作bardrawertoggle mDrawerToggle;
    私人抽屉布局mDrawerLayout;
    私有列表视图mDrawerListView;
    私有视图MFFragmentContainerView;
    private int mCurrentSelectedPosition=0;
    私有布尔值mFromSavedInstanceState;
    私有布尔mUserLearnedDrawer;
    公众出票人(){
    }
    @凌驾
    创建时的公共void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    SharedReferences sp=首选项管理器
    .GetDefaultSharedReferences(getActivity());
    mUserLearnedDrawer=sp.getBoolean(预用户已学习抽屉,false);
    如果(savedInstanceState!=null){
    mCurrentSelectedPosition=savedInstanceState
    .getInt(表示所选位置);
    mFromSavedInstanceState=true;
    }
    //选择默认项(0)或最后选定的项。
    selectItem(mCurrentSelectedPosition);
    }
    @凌驾
    已创建ActivityState上的公共无效(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    //表示此片段希望影响一组
    //操作栏中的操作。
    设置选项菜单(真);
    }
    @凌驾
    创建视图上的公共视图(布局、充气机、视图组容器、,
    Bundle savedInstanceState){
    mDrawerListView=(ListView)充气器。充气(
    R.布局图(抽屉、容器、假);
    mDrawerListView
    .setOnItemClickListener(新的AdapterView.OnItemClickListener(){
    @凌驾
    public void onItemClick(AdapterView父级、视图、,
    内部位置,长id){
    选择项目(位置);
    }
    });
    setAdapter(新的ArrayAdapter(getActionBar())
    .getThemedContext(),android.R.layout.simple\u list\u item\u 1,
    android.R.id.text1,新字符串[]{
    getString(R.string.title_第1节),
    getString(R.string.title_第2节),
    getString(R.string.title_section3),});
    
    <FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="GJ.helpinghand.Drawer$PlaceholderFragment" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
    </FrameLayout>
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="GJ.helpinghand"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="15"
            android:targetSdkVersion="21" />
    
        <permission android:name="WRRITE_EXTERNAL_STORAGE"></permission>    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".Splash"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".Login"
                android:label="@string/title_activity_login" >
            </activity>
            <activity
                android:name=".Home"
                android:label="@string/title_activity_home" >
            </activity>
    
    
            <activity
                android:name=".Drawer"
                android:label="@string/title_activity_drawer" >
            </activity>
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
            </activity>
        </application>
    
    </manifest>
    
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout 
        android:id="@+id/core"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView1"
            android:background="@android:color/darker_gray"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="@string/file"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <ListView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/textView1"
            android:divider="@android:color/holo_orange_light" />
    
    </RelativeLayout>
        <fragment
            android:id="@+id/drawer_frag"
            android:name="com.fragment.NavigationDrawerFragment"
            android:layout_width="@dimen/nav_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            tools:layout="@layout/fragment_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
    
    <FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="GJ.helpinghand.Drawer$PlaceholderFragment" >
    
        <ListView
            android:id="@+id/drawerList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </FrameLayout>
    
    View drawer= inflater.inflate(
                R.layout.fragment_drawer, false);
    mDrawerListView = (ListView)drawer.findViewById(R.id.drawerList);
    .....Your code
    return drawer;