簡體   English   中英

android.view.InflateException誇大類片段的錯誤

[英]android.view.InflateException Error inflating class fragment

我正嘗試從5天開始解決此問題,但仍然沒有解決。

主要Java代碼

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)

當我從導航抽屜中單擊主菜單項時,它可以正常工作。 但是當我在之后單擊“主頁”時,出現此異常

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

Java代碼中是否有任何更正? 請幫助我解決這個問題。
謝謝

將以下變量添加到您的Fragment類中:

private static View view;

然后在相同的片段類中嘗試替換onCreateView方法:

@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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM