【问题标题】:null pointer exception on addmarker and markeroptions on googleMapgoogleMap 上的 addmarker 和 markeroptions 的空指针异常
【发布时间】:2016-11-17 10:06:21
【问题描述】:

尝试在我们的应用程序上加载谷歌地图时出现 nullpointerException

D/Profileactivity: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference

这里是我们得到异常的地方,在 placeMarker 方法中

public void placeMarker(LatLongDetails user_latlongobj2,
                        final Context contextPlace) {
    try {
        if (googlemap == null) {
            intialiseMap();
            animatecamera(user_latlongobj);
        }
        if (LoginDetails.Address.length() < 1) {
            LoginDetails.Address = "Getting your location .....";
        }
        //googlemap.clear();
        marker = new MarkerOptions().position(
                new LatLng(user_latlongobj2.user_latitude,
                        user_latlongobj2.user_longitude)).title(
                LoginDetails.Address);

        System.out.println("This is the Address" + LoginDetails.Address);

        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));

        if (googlemap != null) {

            googlemap.addMarker(marker).showInfoWindow();
        }else {
            intialiseMap();
            googlemap.addMarker(marker).showInfoWindow();
        }
        System.out.println("PLACING MARKER" + LoginDetails.Address);
        if (marker == null || contextPlace == null) {
            Intent in =new Intent(this,ProfileActivity1.class);
            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(in);
        }
        else
            fixmarker(marker, contextPlace);

    } catch (Exception e) {
        fixmarker(marker, contextPlace);
        Log.d("Profileactivity", "" + e);
    }

}

这就是我们初始化地图的方式

private void intialiseMap() {

try {
if (dialog == null)
    dialog = ProgressDialog.show(ProfileActivity1.this, "",
            getString(R.string.getting_location), true, true);
}catch(Exception e) {
Log.e("eybaba",""+e);
}
    try {
        if (googlemap == null) {
            googlemap = ((MapFragment) getFragmentManager()
                    .findFragmentById(R.id.mapfragment)).getMap();
            googlemap.setInfoWindowAdapter(new CustomInfowindow(context));
            // check if map is created successfully or not
            if (googlemap == null) {
                Toast.makeText(getApplicationContext(),
                        R.string.maps_create_error, Toast.LENGTH_SHORT)
                        .show();
            }
        }
    } catch (Exception e) {
    }
}

【问题讨论】:

  • 我猜intialiseMap(); 不起作用或返回新地图,因此调用应该是googlemap = intialiseMap();。基本上错误信息会告诉你出了什么问题:NullPointerException.
  • 第一个警告显示user_longitudeLatLongDetails 是您通过实例user_latlongobj2.user_longitude 访问的类变量,但具有static 修饰符。你可以简单地删除static
  • @Thomas google= intialiseMap();给出不兼容的类型错误
  • else { intialiseMap(); googlemap.addMarker(marker).showInfoWindow(); } googlemap 为null 时,这段代码被执行。参考@Thomas 评论,您需要以some 方式对其进行初始化。
  • @QBrute 我在问问题之前添加了这一行,以检查这是否会解决空指针异常

标签: java android google-maps google-maps-markers google-maps-android-api-2


【解决方案1】:

根据要求的答案总结解决方案和找到它的过程:

1) NullPointerException 和相应的消息表明问题出在此块的 else 分支中:

    if (googlemap != null) {
        googlemap.addMarker(marker).showInfoWindow();
    }else {
        intialiseMap();
        googlemap.addMarker(marker).showInfoWindow();
    }

这里,intialiseMap(); 似乎无法初始化地图。

2) 在intialiseMap() 中有一个try-catch-block,如果为null,则googlemap 应该被初始化。但是,catch-block 是空的,因此尝试初始化时的任何异常都会丢失。

未来读者的注意事项:如果你捕捉到一个异常,你应该总是,总是,总是以某种方式处理它。至少做某事的简单方法之一就是记录它。

当然,在某些情况下您只想忽略特定异常,但在这种情况下,您应该真正了解后果(抛出该异常时会发生什么,为什么会抛出等)并且您始终应该记录您故意忽略该异常,例如在您的代码中添加简短的注释。

3) 记录捕获的异常后,OP 意识到 googlemap 的初始化失败,因此能够进一步跟踪问题。

然后他搜索了答案和解决方案,并想出了以下两个线程来帮助他解决问题:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-19
    • 2021-01-19
    • 2019-07-18
    • 2013-08-12
    • 2015-03-27
    • 2019-12-09
    • 1970-01-01
    相关资源
    最近更新 更多