2014-12-31 37 views
2

我想添加一个标记到谷歌地图在我的android应用程序。但我运行应用程序时,我得到一个空点异常。我没有看到我的地方代码可能出错了。向谷歌地图添加标记的空指针异常

这是我用过

GoogleMap googleMap = null; 

      MapFragment fm = (MapFragment) (activity.getFragmentManager()) 
        .findFragmentById(R.id.map); 

      googleMap = fm.getMap(); 
      googleMap.addMarker(new MarkerOptions() 
      .position(new LatLng(7.421226f,80.401264f)) 
      .title("Hello world")); 

logcat的代码

12-31 16:51:54.667: E/AndroidRuntime(10191): FATAL EXCEPTION: main 
12-31 16:51:54.667: E/AndroidRuntime(10191): java.lang.NullPointerException 
12-31 16:51:54.667: E/AndroidRuntime(10191): at com.fortuna.cinemalk.adapter.ViewPageAdapter.instantiateItem(ViewPageAdapter.java:201) 
12-31 16:51:54.667: E/AndroidRuntime(10191): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:829) 
12-31 16:51:54.667: E/AndroidRuntime(10191): at android.support.v4.view.ViewPager.populate(ViewPager.java:979) 
12-31 16:51:54.667: E/AndroidRuntime(10191): at android.support.v4.view.ViewPager.populate(ViewPager.java:911) 
12-31 16:51:54.667: E/AndroidRuntime(10191): at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:440) 
12-31 16:51:54.667: E/AndroidRuntime(10191): at com.fortuna.cinemalk.TheaterDetailFragment$BackGround.onPostExecute(TheaterDetailFragment.java:148) 
12-31 16:51:54.667: E/AndroidRuntime(10191): at com.fortuna.cinemalk.TheaterDetailFragment$BackGround.onPostExecute(TheaterDetailFragment.java:1) 
+0

的'NPE'在ViewPageAdapter.java:201,该类的第201行是什么 – user2450263

+0

googleMap = fm.getMap(); – CraZyDroiD

回答

0

显然,这个问题必须与地图本身,而不是标记。

首先,检查您正在用作库的Google Play服务的版本。 如果您在6.5 version(2014年12月发布)之前有某些内容,请查看以下代码(在google play服务示例中获得积分)以确保您没有空地图对象。

/* 
* Copyright (C) 2012 The Android Open Source Project 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

package com.example.mapdemo; 

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

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 

/** 
* This shows how to create a simple activity with a map and a marker on the map. 
* <p> 
* Notice how we deal with the possibility that the Google Play services APK is not 
* installed/enabled/updated on a user's device. 
*/ 
public class BasicMapDemoActivity extends FragmentActivity { 
    /** 
    * Note that this may be null if the Google Play services APK is not available. 
    */ 
    private GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.basic_demo); 
     setUpMapIfNeeded(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
    } 

    /** 
    * 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) getSupportFragmentManager().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(new LatLng(0, 0)).title("Marker")); 
    } 
} 



其他解决方案,我强烈建议,是让谷歌最新的Play服务库(检查here)。然后,您必须根据来自谷歌的this guide更改您的代码,使用getMapAsync()(而不是getMap())和回调函数,只要地图准备好可以使用,并且没有任何问题,您可以执行任何操作。

0

编辑:至于其他的答案也提到,其实getMap()现在贬值

因此,不再推荐下面的方法,但可能仍然有效。


首先确保您输入您的Maps API密钥您AndroidManifest.xml

<meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="api-key-here" /> 

然后确保地图在布局中正确定义(见下面的例子)

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<fragment 
    android:id="@+id/mapView" 
    android:layout_width="match_parent" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" /> 

</RelativeLayout> 

一旦完成后,您可以按如下方式访问地图

android.support.v4.app.FragmentManager mFragmentManager = getSupportFragmentManager(); 
final SupportMapFragment mMapFragment = (SupportMapFragment) mFragmentManager.findFragmentById(R.id.mapView); 
googleMap = mMapFragment.getMap(); 
0

getMap已弃用,请改用getMapAsync。你的Activity需要实现OnMapReadyCallback接口,所以你可以在onMapReady方法中添加标记。

从文档:

的GetMap() 此方法已。改为使用getMapAsync(OnMapReadyCallback)。该回调方法为您提供了一个GoogleMap实例,该实例保证为非null并且可以使用。

1

1)该片段可能为空。

除非地图片段在布局中定义,否则您无法在onCreate中找到它。 (如果是这种情况,请告诉我,我会更新答案。)

2)片段尚未加载映射。

这是更可能的情况。您可以有一个MapFragment的实例,但它不必具有GoogleMap的实例。最新的Google Play服务SDK(v6.5.87)为您提供了一种方法MapFragment.getMapAsync(OnMapReadyCallback)。只要GoogleMap实例可用,回调就会运行。只有这样你才能使用它。

0
  1. 首先检查谷歌地图为空或NOTNULL使用此方法 - >如果(!GMAP = NULL)
  2. 无论你要下载标记图标形式的URL(或)加标记图像标记信息窗口直接从url.its中获取网络主线程异常。
  3. 网络主线程异常导致标记图像空这样你就可以在谷歌地图获得空指针异常

解决方案:

添加以下行您的onCreate方法

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

StrictMode.setThreadPolicy(policy);