gpt4 book ai didi

android - "Error inflating class fragment"尝试将 map v2 fragment 添加到 activity_main

转载 作者:行者123 更新时间:2023-11-30 01:09:55 27 4
gpt4 key购买 nike

当我添加 map fragment 时:

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

我已经包含在 depedencies 中

  • com.google.android.gms:play-services:9.2.1和
  • com.google.android.gms:play-services-maps:9.2.1

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

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

提前谢谢你。

最佳答案

<meta-data>元素必须在 <application> 内你的元素AndroidManifest.xml但在 <activity> 之外元素。

关于android - "Error inflating class fragment"尝试将 map v2 fragment 添加到 activity_main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38514653/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com