在 Android 的 Fragment 中使用 Google 地图

Posted

技术标签:

【中文标题】在 Android 的 Fragment 中使用 Google 地图【英文标题】:Using Google Maps inside a Fragment in Android 【发布时间】:2015-09-22 01:59:12 【问题描述】:

我正在尝试在片段中加载 Google 地图,并且无论我尝试实施什么解决方案,我都会不断收到相同的错误。我已经完成了所有类似(如果不相同)的问题,并尝试按照前几个最佳答案的建议去做,但遗憾的是没有运气。因此,如果您能帮助我,我将不胜感激: 我的MainActivity.java如下:

package com.nzf.nzf.nzfmap;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.*;
import android.support.v4.view.ViewPager;
import android.util.Log;

import java.util.List;
import java.util.Vector;

/**
 * Created by Adil on 21/04/2015.
 */
public class MainActivity extends FragmentActivity 

    private PagerAdapter mPagerAdapter;
    public static FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        Log.d("Position", "I am in MainActivity");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // initialising the object of the FragmentManager.
        fragmentManager = getSupportFragmentManager();
        initialisePaging();
    
    private void initialisePaging() 
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(new ListFragment());
        fragments.add(new MapsFragment());
        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager(),fragments);
        pager.setAdapter(mPagerAdapter);
    


我将两个片段添加到片段列表中 1. ListFragment 我对这个片段没有任何问题。 2. MapsFragment 我主要关心的是这个片段。 这是我的MapsFragment.java 内容:

package com.nzf.nzf.nzfmap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapsFragment extends Fragment 
    static final LatLng MELBOURNE = new LatLng(-37.8254,144.95410);
    static final LatLng SYDNEY = new LatLng(-33.86916,151.20437);

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    public MapsFragment() 
        // Required empty public constructor
    

    @Override
    public void onResume() 
        super.onResume();
        setUpMapIfNeeded();
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        Log.d("Position", "I am in MapsFragment");
        if (container == null) 
            return null;
        
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_map, container, false);

        setUpMapIfNeeded();

        return view;
    
    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call @link #setUpMap() once when @link #mMap is not null.
     * <p/>
     * If it isn't installed @link SupportMapFragment (and
     * @link com.google.android.gms.maps.MapView MapView) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), @link #onCreate(Bundle) may not be called again so we should call this
     * method in @link #onResume() to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() 
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) 
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) 
                setUpMap();
            
    

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that @link #mMap is not null.
     */
    private void setUpMap() 
        mMap.addMarker(new MarkerOptions().position(MELBOURNE).title("mMarker"));
        MarkerOptions mo = new MarkerOptions();
        mo.draggable(true);
        mo.position(SYDNEY).title("sMarker");
        mo.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
        mMap.addMarker(mo);

        Log.d(Constants.LOG, "on setUpMap");

    
    public interface Constants 
        String LOG = "com.nzf.nzf.nzfmap";
    


上述片段的Xml文件(fragment_map.xml)如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="com.nzf.nzf.nzfmap.MapsFragment">

<fragment
    android:id="@+id/map"
    android:layout_
    android:layout_
    class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

我在 logcat 中遇到的错误如下:

07-04 21:32:37.455    7623-7623/com.example.nzf.nzfmap D/Position﹕ I am in MainActivity
07-04 21:32:37.530    7623-7623/com.example.nzf.nzfmap D/dalvikvm﹕ GC_FOR_ALLOC freed 103K, 17% free 10022K/12000K, paused 23ms, total 23ms
07-04 21:32:37.535    7623-7623/com.example.nzf.nzfmap I/dalvikvm-heap﹕ Grow heap (frag case) to 12.689MB for 1127536-byte allocation
07-04 21:32:37.565    7623-7632/com.example.nzf.nzfmap D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 16% free 11123K/13104K, paused 24ms, total 24ms
07-04 21:32:37.615    7623-7623/com.example.nzf.nzfmap D/Position﹕ I am in ListFragment
07-04 21:32:37.615    7623-7623/com.example.nzf.nzfmap D/Position﹕ I am in MapsFragment
07-04 21:32:37.630    7623-7623/com.example.nzf.nzfmap I/x﹕ Making Creator dynamically
07-04 21:32:37.650    7623-7623/com.example.nzf.nzfmap I/Google Maps Android API﹕ Google Play services client version: 6587000
07-04 21:32:37.655    7623-7623/com.example.nzf.nzfmap I/Google Maps Android API﹕ Google Play services package version: 7574036
07-04 21:32:37.675    7623-7623/com.example.nzf.nzfmap I/dalvikvm﹕ Could not find method android.app.AppOpsManager.checkPackage, referenced from method com.google.android.gms.common.ij.a
07-04 21:32:37.675    7623-7623/com.example.nzf.nzfmap W/dalvikvm﹕ VFY: unable to resolve virtual method 111: Landroid/app/AppOpsManager;.checkPackage (ILjava/lang/String;)V
07-04 21:32:37.675    7623-7623/com.example.nzf.nzfmap D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0012
07-04 21:32:37.675    7623-7623/com.example.nzf.nzfmap I/dalvikvm﹕ Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.ij.a
07-04 21:32:37.675    7623-7623/com.example.nzf.nzfmap W/dalvikvm﹕ VFY: unable to resolve virtual method 449: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
07-04 21:32:37.680    7623-7623/com.example.nzf.nzfmap D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000d
07-04 21:32:37.685    7623-7623/com.example.nzf.nzfmap I/dalvikvm﹕ Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.ij.b
07-04 21:32:37.685    7623-7623/com.example.nzf.nzfmap W/dalvikvm﹕ VFY: unable to resolve virtual method 154: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
07-04 21:32:37.685    7623-7623/com.example.nzf.nzfmap D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0201
07-04 21:32:37.895    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ Get MotionRecognitionManager
07-04 21:32:37.900    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ onVisibilityChanged() is called, visibility : 8
07-04 21:32:37.900    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ unregisterIRListener() is called
07-04 21:32:37.905    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ onVisibilityChanged() is called, visibility : 8
07-04 21:32:37.905    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ unregisterIRListener() is called
07-04 21:32:37.975    7623-7668/com.example.nzf.nzfmap D/dalvikvm﹕ GC_FOR_ALLOC freed 558K, 17% free 12102K/14532K, paused 31ms, total 31ms
07-04 21:32:38.080    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
07-04 21:32:38.080    7623-7623/com.example.nzf.nzfmap D/AbsListView﹕ unregisterIRListener() is called
07-04 21:32:38.110    7623-7623/com.example.nzf.nzfmap D/AndroidRuntime﹕ Shutting down VM
07-04 21:32:38.110    7623-7623/com.example.nzf.nzfmap W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41bae700)
07-04 21:32:38.115    7623-7623/com.example.nzf.nzfmap E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.nzf.nzf.nzfmap.MapsFragment.setUpMapIfNeeded(MapsFragment.java:66)
            at com.nzf.nzf.nzfmap.MapsFragment.onCreateView(MapsFragment.java:43)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:16831)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16831)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16831)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:16831)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
            at android.view.View.measure(View.java:16831)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2189)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1352)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1535)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
            at android.view.Choreographer.doCallbacks(Choreographer.java:591)
            at android.view.Choreographer.doFrame(Choreographer.java:561)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)

编辑 1:这是我的 AndroidManifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nzf.nzf.nzfmap" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
 The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name="com.nzf.nzf.nzfmap.MainActivity"
            android:label="@string/title_activity_maps" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

你需要获取最新的googlplayservices 谢谢你的回答,你知道如何更新现有的吗? 您可以从 google play store 更新它...您需要使用物理设备.. 你能粘贴你的 AndroidManifest.xml 文件吗 似乎 (SupportMapFragment)getFragmentManager() 或 (SupportMapFragment)getFragmentManager().findFragmentById(R.id.map) 必须返回 null,您能否进行一些调试以确定它是哪一个是,让我们知道是哪一个? 【参考方案1】:

您的 MapsFragment 的第 66 行,替换:

// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map))
    .getMap();

作者:

// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map))
    .getMap();

因为您的地图片段是片段中的片段,而不是由*** FragmentManager 管理。

另外,我不确定,但您可能希望异步设置地图:

    ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(
    new OnMapReadyCallback() 
        @Override
        public void onMapReady(GoogleMap googleMap) 
            mMap = googleMap;
            setUpMap();
        
    );

【讨论】:

这真的很奇怪,我之前尝试过使用 getChildFragmentManager() 但是这次它对我有用。还是谢谢【参考方案2】:

您可以像普通片段一样执行此操作。

public void replaceFragment(Fragment fragment, String fragmentTag)
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, fragment);
            fragmentTransaction.addToBackStack(fragmentTag);
            fragmentTransaction.commit();
        

private void initMap()
        MapFragment mapFragment = MapFragment.newInstance();
        replaceFragment(mapFragment, fragmentTag);
        mapFragment.getMapAsync(this);
    

【讨论】:

以上是关于在 Android 的 Fragment 中使用 Google 地图的主要内容,如果未能解决你的问题,请参考以下文章

android中fragment的简单使用

Android碎片Fragment总结

Android中Fragment的使用

请问Android Fragment该怎么使用呢?

如何在 Android 的 Fragment 中使用 setUserVisibleHint

如何在Fragment中使用Actionbar-Android开发问答