【发布时间】:2019-07-07 22:46:44
【问题描述】:
我想了解更多关于 Google Maps API 的信息,所以我决定创建一个 Google Maps Activity 类型的项目。创建项目后,无需更改代码中的任何内容,我立即收到消息:
错误:清单合并失败:属性 应用程序@appComponentFactory 值=(android.support.v4.app.CoreComponentFactory)来自 [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 也出现在 [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 值=(androidx.core.app.CoreComponentFactory)。建议:加 'tools:replace="android:appComponentFactory"' 到元素 在 AndroidManifest.xml:12:5-41:19 处覆盖。
我已按照 https://developers.google.com/maps/documentation/android-sdk/start 的教程进行操作,因此我使用最新的可用 SDK 更新了环境并生成了 API 密钥(我已将其插入 google_maps_api.xml)。
我从您的网站上找到的可能解决方案是在 app 模块 build.gradle 的末尾添加以下代码段
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '25.3.0'
}
}
}
}
但在那之后,我得到了错误:
不可转换的类型;无法将“android.support.v4.app.Fragment”转换为 'com.google.android.gms.maps.SupportMapFragment'
以下行:
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
这是我的 activity_maps.xml 和 MapsActivity.java 的样子:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
package com.example.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));
}
}
此刻我真的很绝望,因为我已经尝试了我所知道的一切。我本来希望从 Android studio 提供的这个 Hello world 项目开始,构建一些更复杂的东西,但它似乎不起作用。拜托,我真的需要帮助。提前致谢
【问题讨论】:
-
让我看看你的 AndroidManifest.xml 文件
-
Felipe,这里是 Google Drive 共享的链接。由于AndroidManifest.xml内容太大,无法评论,我已经上传到Google Drive:drive.google.com/open?id=1HuNRFfGP5nkpaoOOMDzaViZbw0XmhX0m
-
您可以编辑您的问题以包含更多信息。我没有看到你的清单有什么问题,让我看看你的活动和片段布局
-
或许本教程能帮到你:code.luasoftware.com/tutorials/android/…
-
Felipe,我已经编辑了问题,因此您可能会发现活动和片段布局的样子。我也会看看教程:)
标签: android