gpt4 book ai didi

android - 不可转换类型;无法将 'androidx.fragment.app.Fragment' 转换为 'com.google.android.gms.maps.MapFragment'

转载 作者:行者123 更新时间:2023-12-04 15:43:16 24 4
gpt4 key购买 nike

在我的 fragment 中:

 import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

public class AgentsFragmentMapTab extends Fragment {

com.google.android.gms.maps.MapFragment mapFragment = (com.google.android.gms.maps.MapFragment) getFragmentManager().findFragmentById(R.id.google_map)

此处布局:

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

<fragment
android:id="@+id/google_map"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
class="com.google.android.gms.maps.MapFragment"/>

</LinearLayout>

但是我得到了编译错误:

Inconvertible types; cannot cast 'androidx.fragment.app.Fragment' to 'com.google.android.gms.maps.MapFragment'

最佳答案

MapFragment 现在仅在您的应用面向 api 12 及更高版本时使用。 https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment .您可以考虑改用 SupportMapFragment。

在你的布局文件中

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
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="com.example.mapwithmarker.MapsMarkerActivity" />

在你的 Activity 中

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve the content view that renders the map.
setContentView(R.layout.activity_maps);
// Get the SupportMapFragment and request notification
// when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

您可以在开发者网站上获得更多信息:https://developers.google.com/maps/documentation/android-sdk/map-with-marker

关于android - 不可转换类型;无法将 'androidx.fragment.app.Fragment' 转换为 'com.google.android.gms.maps.MapFragment',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56915408/

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