本文介绍了NullPointerException异常,而试图折线/标记添加到的GoogleMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GoogleMap对象(这是我使用的是GoogleMapOptions已经操作了)一个SupportMapFragment,它会显示/运行正常,直到我打电话的GetMap()。addPolyline(...),我得到一个NullPointerException异常。

I have a SupportMapFragment with a GoogleMap object (which I have manipulated already using a GoogleMapOptions) and it is displaying/functioning fine, until I call getMap().addPolyline(...) where I get a NullPointerException.

下面是我对活动布局的xml:

Here is my layout xml for the Activity:

<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" >

<TextView
    android:id="@+id/tripSummary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/trip_summary"
    tools:context=".Start" />

<TextView
    android:id="@+id/tripDetails"
    android:layout_below="@id/tripSummary"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/awaiting_location"
    tools:context=".Start" />

 <fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:layout_below="@id/tripDetails"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:cameraZoom="13"
    android:name="com.google.android.gms.maps.SupportMapFragment"  />
</RelativeLayout>

下面是我在活动中的createMap方法(它扩展android.support.v4.app.FragmentActivity),这就是所谓的onCreate(...)

Here is the createMap method in my Activity (which extends android.support.v4.app.FragmentActivity) which is called in onCreate(...)

private void createMap(List<LatLng> latLngs) {

    float zoom = 13;
    PolylineOptions poly = new PolylineOptions()
    .color(Color.RED);
    MarkerOptions startMarker = new MarkerOptions()
    .position(latLngs.get(0))
    .title("Start");
    MarkerOptions endMarker = new MarkerOptions()
    .position(latLngs.get(latLngs.size() - 1))
    .title("End");
    LatLng startLatLng = latLngs.get(0);
    GoogleMapOptions mapOptions = new GoogleMapOptions();
    CameraPosition cameraP = new CameraPosition(startLatLng, zoom, 0, 0);

    if (mMap == null) {

        // instantiate map
        mMapFragment = SupportMapFragment.newInstance(mapOptions);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
        fragmentTransaction.add(R.id.map, mMapFragment);
        fragmentTransaction.commit();
        Log.d("Map", "fragment transaction complete");

        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        // check it has been instantiated
        if (mMapFragment != null) {

            mapOptions.mapType(GoogleMap.MAP_TYPE_SATELLITE)
            .camera(cameraP)
            .compassEnabled(true);


            //Add LatLngs to a polyline
            for(LatLng latLng : latLngs){
                poly.add(latLng);
            }

            mMapFragment.getMap().addPolyline(poly);
            mMapFragment.getMap().addMarker(startMarker);
            mMapFragment.getMap().addMarker(endMarker);
        }
    }
}

LogCat中:

LogCat:

01-03 20:42:42.737: E/AndroidRuntime(18116): FATAL EXCEPTION: main
01-03 20:42:42.737: E/AndroidRuntime(18116): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.locationapp/com.project.locationapp.TripSummary}: java.lang.NullPointerException
01-03 20:42:42.737: E/AndroidRuntime(18116):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at android.os.Looper.loop(Looper.java:130)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at android.app.ActivityThread.main(ActivityThread.java:3701)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at java.lang.reflect.Method.invokeNative(Native Method)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at java.lang.reflect.Method.invoke(Method.java:507)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at dalvik.system.NativeStart.main(Native Method)
01-03 20:42:42.737: E/AndroidRuntime(18116): Caused by: java.lang.NullPointerException
01-03 20:42:42.737: E/AndroidRuntime(18116):    at com.project.locationapp.TripSummary.createMap(TripSummary.java:113)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at com.project.locationapp.TripSummary.onCreate(TripSummary.java:38)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-03 20:42:42.737: E/AndroidRuntime(18116):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
01-03 20:42:42.737: E/AndroidRuntime(18116):    ... 11 more

我已经测试了用于获取经纬度值的数据源,并检索值的罚款。任何建议,为什么我收到一个 NullPointerException异常试图从地图时的 SupportMapFragment

先谢谢了。

推荐答案

如果您添加的 SupportMapFragment 应用于布局文件您不必手动创建一个新的实例。你只需要通过调用获得参考

If you add the SupportMapFragment to the layout file you don't have to create a new instance manually. You just need to obtain the reference by calling

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap map = mapFragment.getMap();

您很可能得到NPE,因为新实例片段还没有创建GoogleMap对象呢。

You are probably getting the NPE because the newly instantiated fragment haven't created the GoogleMap object yet.

这篇关于NullPointerException异常,而试图折线/标记添加到的GoogleMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 01:26