尝试将map v2片段添加到activity_main时“填充类片段错误”

时间:2016-07-21 21:27:21

标签: android google-maps android-fragments

当我添加地图片段时:

<fragment 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:id="@+id/map"
          tools:context=".MainActivity"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>

到activity_main.xml我收到了这个错误:

FATAL EXCEPTION: main
Process: com.example.koba.ogloszenia, PID: 3073
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.koba.ogloszenia/com.example.koba.ogloszenia.MainActivity}: android.view.InflateException: Binary XML file line #120: Error inflating class fragment 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
...

我的 MainActivity.java

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SupportMapFragment MapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
    MapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap Map) {
    mMap = Map;
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

我的 AndroidManifest.xml

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

<uses-permission android:name="android.Manifest.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.Manifest.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.Manifest.permission.WRITE_EXTERNAL_STORAGE" />

<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">

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="MY_API_KEY"/>

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

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

我已经包含在依赖

  • com.google.android.gms:发挥服务:9.2.1 和
  • com.google.android.gms:发挥服务,地图:9.2.1

我还在SDK工具中检查了“Google Play服务”。

我也看过类似的帖子,但这并没有解决我的问题。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

<meta-data>元素必须位于<application>的{​​{1}}元素内,但必须位于AndroidManifest.xml元素之外。