【问题标题】:Error inflating class fragment in simple Google Maps project [duplicate]在简单的谷歌地图项目中膨胀类片段时出错[重复]
【发布时间】:2016-04-01 14:20:19
【问题描述】:

我只想运行一个简单的地图片段,但我不断收到 InflateException。我也问了一个类似的问题,但为了简单起见,我创建了一个新的 Google 地图项目。

04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: Process: com.example.ali.myapplication, PID: 1785
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ali.myapplication/com.example.ali.myapplication.MapsActivity}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-01 14:14:20.109 1785-1785/com.example.ali.myapplication E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment

MapsActivity.java:

package com.example.ali.myapplication;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

activity_maps.xml:

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#619ec9"
    xmlns:android="http://schemas.android.com/apk/res/android">
    >

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map" tools:context=".MapsActivity"
        class="com.google.android.gms.maps.SupportMapFragment"
        tools:layout="@layout/activity_maps"
        android:layout_width="362dp"
        android:layout_height="323dp"
        />


</LinearLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ali.myapplication" >

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >

        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />


        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

  • 一个小的可复制案例是个好主意,但我认为您可以修改您现有的问题,因为它本质上是在问同样的问题。

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


【解决方案1】:

Fragment 应该添加到框架层中,不能直接使用。 更新您的代码,让我知道是否可行

【讨论】:

  • 您好,感谢您的回答。我添加了 LinearLayout 但我仍然得到同样的错误
猜你喜欢
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多