gpt4 book ai didi

android - 另一个 fragment 内的谷歌地图 - 调用 getMapAsync 时出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:21 26 4
gpt4 key购买 nike

我正在尝试将谷歌地图 Activity 转换为在将在 NavigationView 中调用的 fragment 内工作。 map 正在显示,但是当我使用代码 getMapAsync

mFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mFragment.getMapAsync(this);

我收到一条错误消息

E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: java.lang.NullPointerException
E/AndroidRuntime: at com.sample.samplemap.samplemapFragment.onCreate(samplemapFragment.java:32)
E/AndroidRuntime: at android.support.v4.app.Fragment.performCreate(Fragment.java:1942)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5306)
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)

samplemapFragment.java

package  com.sample.samplemap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

public class samplemapFragment extends Fragment implements OnMapReadyCallback {
GoogleMap mGoogleMap;
SupportMapFragment mFragment;

public samplemapFragment() {
Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mFragment.getMapAsync(this);
}

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

@Override
public void onMapReady(GoogleMap googleMap) {

mGoogleMap = googleMap;
mGoogleMap.setMyLocationEnabled(true);
}
}

fragment_sample_map.xml

<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.sample.samplemap.samplemapFragment"
xmlns:tools="http://schemas.android.com/tools">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="384dp"
android:layout_height="300dp" android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_marginTop="44dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

最佳答案

我检查过 fragment 的生命周期是: https://developer.xamarin.com/guides/android/platform_features/fragments/part_1_-_creating_a_fragment/Images/fragment_lifecycle.png

OnCreate 将首先调用 OnCreateView。

因为我已经为您实现了可以在此处与 MapView 一起正常工作的代码:

MapCheckingActivity.java

package com.ramesh.expandablelistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MapCheckingActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_checking);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame,
new SampleMapFragment()).commit();
}

}

SampleMapFragment.java

package com.ramesh.expandablelistview;

/**
* Created by Ramesh Kumar on 2/17/2016.
*/


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;


public class SampleMapFragment extends Fragment implements
OnMapReadyCallback {


GoogleMap mGoogleMap;
MapView mapView;

View mView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapsInitializer.initialize(getContext());
}

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

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mapView = (MapView) mView.findViewById(R.id.map);
if (mapView != null) {
// Initialise the MapView
mapView.onCreate(null);
mapView.onResume();
// Set the map ready callback to receive the GoogleMap object
mapView.getMapAsync(this);
}

}

@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());

mGoogleMap = googleMap;
mGoogleMap.setMyLocationEnabled(true);


}

}

activity_map_checking.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="com.ramesh.expandablelistview.MapCheckingActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_map_checking" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

content_map_checking.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_map_checking"
tools:context="com.ramesh.expandablelistview.MapCheckingActivity">


<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame"/>

</RelativeLayout>

fragment 映射.xml:

<RelativeLayout
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"
tools:context="com.sample.samplemap.samplemapFragment">

<!--<fragment xmlns:android="http://schemas.android.com/apk/res/android"-->
<!--xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="384dp"-->
<!--android:layout_height="300dp" android:id="@+id/map"-->
<!--android:name="com.google.android.gms.maps.SupportMapFragment"-->
<!--android:layout_marginTop="44dp"-->
<!--android:layout_alignParentTop="true" />-->

<com.google.android.gms.maps.MapView
android:id="@+id/map" android:layout_marginTop="44dp"
android:layout_width="384dp" android:layout_height="300dp"/>
</RelativeLayout>

enter image description here

我已经通过 MapView 在 Fragment 中创建了 map 。

在 list 中:

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

map 没有显示是因为我输入错误的 HashKey如果有帮助,请接受它作为答案。谢谢

关于android - 另一个 fragment 内的谷歌地图 - 调用 getMapAsync 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35458584/

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