android.view.InflateException错误充气类片段 [英] android.view.InflateException Error inflating class fragment

查看:257
本文介绍了android.view.InflateException错误充气类片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从5天解决这个问题,但我仍然没有得到它解决。

I am trying to solve this problem from 5 days, but still I didn't get it solved.

主要的Java code

package com.beproject.ourway;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.os.Handler;
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;
import android.widget.Toast;


public class MainHome extends FragmentActivity{
    private String[] mNavigationDrawerItemTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    Fragment fragment = null;
    FragmentManager fragmentManager;
    @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[6];

    drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_launcher, "Home");
    drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_launcher, "Places");
    drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_launcher, "Friends");
    drawerItem[3] = new ObjectDrawerItem(R.drawable.ic_launcher, "Settings");
    drawerItem[4] = new ObjectDrawerItem(R.drawable.ic_launcher, "Help");
    drawerItem[5] = new ObjectDrawerItem(R.drawable.ic_launcher, "About");

    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) {

        fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        getActionBar().setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);

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

public static 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 static 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;
    }

}
}

主要的XML文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

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
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" >
</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>

LogCat中

02-16 20:08:05.714: E/AndroidRuntime(5999): FATAL EXCEPTION: main
02-16 20:08:05.714: E/AndroidRuntime(5999): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at com.beproject.ourway.MainHome$HomeFragment.onCreateView(MainHome.java:105)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.app.BackStackRecord.run(BackStackRecord.java:635)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.os.Handler.handleCallback(Handler.java:615)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.os.Looper.loop(Looper.java:153)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.app.ActivityThread.main(ActivityThread.java:4987)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at java.lang.reflect.Method.invokeNative(Native Method)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at java.lang.reflect.Method.invoke(Method.java:511)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at dalvik.system.NativeStart.main(Native Method)
02-16 20:08:05.714: E/AndroidRuntime(5999): Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f0c0017, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2164)
02-16 20:08:05.714: E/AndroidRuntime(5999):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)

当我点击主页菜单项导航抽屉里,然后它工作正常。但是,当我之后然后再次单击主页上我得到这个异​​常

When I click on Home menu item from navigation drawer then it works fine. But when I click again on Home after then I get this exception

02-16 20:08:05.714: E/AndroidRuntime(5999): android.view.InflateException: Binary XML file line #8: Error inflating class fragment 

有没有在java中code的任何修正?请帮我解决这个问题。
谢谢

Is there any correction in java code? please help me to solve this problem.
Thanks

推荐答案

以下变量添加到您的片段类:

Add the following variable to your Fragment class:

private static View view;

然后在同一片段类尝试更换onCreateView方法:

Then in that same fragment class try replacing the onCreateView Method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
if(view != null)
    {
        ViewGroup parent = (ViewGroup) view.getParent();
        if(parent != null)
        {
            parent.removeView(view);
        }
    }
    try
    {
        view = inflater.inflate(R.layout.fragment_places, container, false);
    }
    catch(InflateException e){
        // map is already there, just return view as it is
    }
    return view;

}

这篇关于android.view.InflateException错误充气类片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆