Android无法将地图添加到片段。 android.view.InflateException:在android.view.LayoutInflater.createViewFromTag中输出类片段时出错

时间:2015-12-07 16:44:57

标签: java android xml google-maps android-fragments

我正在尝试将Google地图添加到我的Android应用中的片段中,并且我收到了android.view.InflateException

我的应用有问题的部分的背景:

  • 我已经使用ViewPager实现了一个TabLayout,其中每个标签都会在ViewPager中显示一个片段
  • 这使用FragmentPagerAdapter
  • 我已经单独测试了API密钥等,可以在我的Android设备上显示基本地图
  • 我尝试在我的XML布局中使用android:name="com.google.android.gms.maps.SupportMapFragment"class="com.google.android.gms.maps.SupportMapFragment"作为地图片段

以下是一些可能相关文件的要点:

https://gist.github.com/ThrowingSpoon/dbd982c5720e4c27ed47

这两个位置的错误跟踪看起来很有希望:

Caused by: android.view.InflateException: Binary XML file line #7: 
Error inflating class fragment at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)

android.view.InflateException: Binary XML file line #7: 
Binary XML file line #7: Error inflating class fragment at 
android.view.LayoutInflater.inflate(LayoutInflater.java:539) at 
android.view.LayoutInflater.inflate(LayoutInflater.java:423) at 
uk.co.liammartin.shout.RespondFragment2.onCreateView(RespondFragment2.java:27)

在此处添加代码:

logcat的:

Process: uk.co.liammartin.shout, PID: 17676
                                                   android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                       at uk.co.liammartin.shout.RespondFragment2.onCreateView(RespondFragment2.java:29)
                                                       at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                       at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                       at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:901)
                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                       at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                                                       at android.view.View.measure(View.java:18788)
                                                       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                                                       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                       at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                    Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                    at

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="uk.co.liammartin.shout">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- Creating Permission to receive Google Maps -->
    <permission
        android:name="uk.co.liammartin.shout.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <!-- Permission to receive Google Maps -->
    <uses-permission android:name="com.arshad.map.permission.MAPS_RECEIVE" />

    <!-- Maps API needs OpenGL ES 2.0. -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="Removed my key but it is here" />
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
        </activity>

        <activity
            android:name=".ShoutFilter"
            android:label="Shout Filter"
            android:theme="@style/Theme.Transparent">
        </activity>

        <activity
            android:name=".NewShout"
            android:label="Create a Shout"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="stateVisible|adjustResize">
        </activity>

        <activity android:name=".RespondToShout">
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="Removed my key but it is here" />
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
        </activity>

    </application>

</manifest>

布局:

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

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#CCFFFFFF">

        <TextView
            android:id="@+id/map_location_name"
            style="@style/Base.TextAppearance.AppCompat.Large"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="6dp"
            android:text="Novo Coffee" />

        <TextView
            android:id="@+id/map_time_distance_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/map_location_name"
            android:layout_centerHorizontal="true"
            android:paddingTop="10dp"
            android:text="2 minutes - 150yd - Cannon St" />

        <Button
            style="@android:style/Widget.Material.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/map_time_distance_location"
            android:layout_centerHorizontal="true"
            android:text="NAVIGATE"
            android:textColor="@color/colorPrimary" />

    </RelativeLayout>
</RelativeLayout>

但不知道我哪里出错了。请帮忙!

1 个答案:

答案 0 :(得分:0)

在onCreateView方法中。

map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

尝试更改:

getFragmentManager()

getChildFragmentManager() or getSupportFragmentManager()

由于您有嵌套片段,您可能需要传入子片段管理器。此外,如果您正在使用支持v4片段,则应始终使用getSupportFragmentManager()而不是getFragmentManager()