如何解决此错误:InflateException: Binary XML file line #11: Error inflating class fragment

Posted

技术标签:

【中文标题】如何解决此错误:InflateException: Binary XML file line #11: Error inflating class fragment【英文标题】:How to fix it this error : InflateException: Binary XML file line #11: Error inflating class fragment 【发布时间】:2016-01-27 18:14:02 【问题描述】:

我找不到其他类似此类错误的解决方案。我收到此错误: 例外:

10-28 09:47:35.819: E/androidRuntime(7884): android.view.InflateException: Binary XML file line #11: Error inflating class fragment
10-28 10:04:25.999: E/AndroidRuntime(9471):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-28 10:04:25.999: E/AndroidRuntime(9471):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-28 10:04:25.999: E/AndroidRuntime(9471):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
10-28 10:04:25.999: E/AndroidRuntime(9471):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-28 10:04:25.999: E/AndroidRuntime(9471):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)

我尝试过去 4 天,但找不到任何解决方案。请帮我。

请在下面找到我的代码

谢谢你:)

XML :-

<RelativeLayout
    android:layout_
    android:layout_
    android:orientation="vertical" >

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_
        android:layout_
        android:layout_above="@+id/adView" />

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_
        android:layout_
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="xxxxxxxxxxxxxxxxxxxxxx" />

    <com.mst.app.adapter.TransparentPanel
        android:id="@+id/popup_window"
        android:layout_
        android:layout_
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:background="@android:color/holo_blue_light"
        android:gravity="left"
        android:orientation="vertical"
        android:padding="1dp" >

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_
            android:layout_
            android:background="@drawable/button_bar_gradient"
            android:gravity="right"
            android:orientation="vertical" >

            <Button
                android:id="@+id/hide_popup_button"
                android:layout_
                android:layout_
                android:layout_alignParentRight="true"
                android:background="@drawable/listview_button"
                android:gravity="center"
                android:padding="2dp"
                android:text="  Close  "
                android:textSize="12sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/Offers"
                android:layout_
                android:layout_
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="Offers"
                android:textColor="@android:color/white"
                android:textStyle="bold" />
        </RelativeLayout>

        <TextView
            android:id="@+id/location_description"
            android:layout_
            android:layout_
            android:layout_margin="5dp"
            android:scrollbars="vertical"
            android:text="Testing offers \n"
            android:textColor="@android:color/white" />
    </com.mst.app.adapter.TransparentPanel>
</RelativeLayout>

Android 代码:-

Intent intent = new Intent(MainActivity.this, TabMapMainFragmentActivity.class);
            startActivity(intent);

TabMapMainFragmentActivity.java

package com.mst.app.view;

import com.mst.app.R;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;


public class TabMapMainFragmentActivity extends FragmentActivity implements TabListener

    private ActionBar actionBar;

    TabMapAllFoodTypeFragment tab1;
    TabMapVegFoodTypeFragment tab2;

    @Override
    protected void onCreate(Bundle arg0) 
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.tab_main_fragment);

        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // adding tabs
        Tab aTab1 = actionBar.newTab();
        aTab1.setText("All");
        aTab1.setTabListener(this);
        actionBar.addTab(aTab1);

        Tab aTab2 = actionBar.newTab();
        aTab2.setText("Veg");
        aTab2.setTabListener(this);
        actionBar.addTab(aTab2);

    

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) 
        // TODO Auto-generated method stub
        int selectedTabIndex = tab.getPosition();
        if (selectedTabIndex == 0) 
        
            tab1 = new TabMapAllFoodTypeFragment();
            ft.replace(R.id.tabPlaceHolderInvoices, tab1);
        
        else if (selectedTabIndex == 1) 
        
            tab2 = new TabMapVegFoodTypeFragment();
            ft.replace(R.id.tabPlaceHolderInvoices, tab2);
        

    

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) 
        // TODO Auto-generated method stub

    

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) 
        // TODO Auto-generated method stub

    


TabMapAllFoodTypeFragment.java

package com.mst.app.view;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mst.app.R;

public class TabMapAllFoodTypeFragment extends Fragment 



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

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


        return rootView;
    





TabMapVegFoodTypeFragment.java

package com.mst.app.view;

import com.mst.app.R;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabMapVegFoodTypeFragment extends Fragment 

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

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

        setHasOptionsMenu(true);

        return rootView;
    

清单

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mst.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="22" />

    <permission
        android:name="com.mst.app.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.mst.app.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_GPS" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/restaurant"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mst.app.view.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.mst.app.view.MapActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name="com.mst.app.view.SplashActivity"
            android:screenOrientation="portrait" />

        <activity
            android:name="com.mst.app.view.TabMapMainFragmentActivity"
            android:screenOrientation="portrait" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="xxxxx" />

        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    </application>

</manifest>

【问题讨论】:

尝试将片段更改为 com.google.android.gms.maps.SupportMapFragment 【参考方案1】:
<fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_
        android:layout_
        android:layout_above="@+id/adView" />

不正确。您必须使用android:name 而不是class。所以正确的应该是:

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_
        android:layout_
        android:layout_above="@+id/adView" />

并确保您在 build.gradle 中添加了 Google Maps 依赖项

【讨论】:

我做到了。结果相同。【参考方案2】:

我认为问题在于您将 Fragment 膨胀到另一个 Fragment 内部。这称为嵌套片段。它的实现与普通的有点不同。您可以将 MapView 用于您的地图。

    <fragment
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_
    android:layout_
    android:layout_above="@+id/adView" />

而不是

<com.google.android.gms.maps.MapView
    android:id="@+id/map"
    android:layout_
    android:layout_
    android:layout_above="@+id/adView" />

然后基本findView这个id并调用.getMap()获取GoogleMap对象。之后都是一样的。

【讨论】:

更改 mapview 后我收到新错误 10-28 14:19:16.286: E/AndroidRuntime(7611): java.lang.RuntimeException: Unable to start activity ComponentInfocom.mst.app/com .mst.app.view.MapActivity:java.lang.NullPointerException

以上是关于如何解决此错误:InflateException: Binary XML file line #11: Error inflating class fragment的主要内容,如果未能解决你的问题,请参考以下文章

android.view.InflateException Binary xml 文件第 11 行:错误膨胀类片段

java.lang.reflect.InvocationTargetException - android.view.InflateException: Binary XML file line #1

InflateException - 浮动操作按钮

android.view.InflateException:二进制 XML 文件第 15 行:二进制 XML 文件第 19 行:膨胀类片段时出错

包含布局时如何解决 inflateException 问题?

Android自定义View出现android.view.InflateException的一种解决方法