gpt4 book ai didi

Fragment 中的 Android Google Map 始终显示 SupportMapFragment 为空

转载 作者:行者123 更新时间:2023-11-29 23:28:53 26 4
gpt4 key购买 nike

我想构建一个 android map 应用程序,Launch 应用程序将从 SupportMapFragment 获取 null(说我的 supportMapFragment 为 null),如果你能指出我的错误在哪里,请帮助,非常感谢。代码、XML 和 list 如下。

顺便说一下,MapFragment 来自 BottomNavigationView 标签

        R.id.navigation_map -> {
val mapFragment = MapFragment()
mapFragment.setArguments(intent.extras)
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, mapFragment).commit()
return@OnNavigationItemSelectedListener true

map fragment .java

public class MapFragment extends Fragment implements OnMapReadyCallback {

private View rootView;
private AppCompatActivity context;
private SupportMapFragment supportMapFragment;
private GoogleMap map;


public MapFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_map, container, false);

context = (AppCompatActivity) getActivity();
supportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.fragmentmap);

if (supportMapFragment != null) {
supportMapFragment.getMapAsync(this);
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, supportMapFragment).commit();
} else {
Toast.makeText(context, "Error - Map Fragment was null!!", Toast.LENGTH_SHORT).show();
}
return rootView;
}

@Override
public void onMapReady(GoogleMap googleMap) {

}
}

fragment 映射.xml

   <android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true">

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentmap"
tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</android.support.design.widget.CoordinatorLayout>

list

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

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

</application>

最佳答案

如果您在另一个 fragment 中使用 fragment ,那么最好使用 getChildFragmentManager,因为它返回一个私有(private)的 FragmentManager 用于放置和管理 Fragments这个 fragment

supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragmentmap);

Reference: getChildFragmentManager

关于Fragment 中的 Android Google Map 始终显示 SupportMapFragment 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53059054/

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