为GoogleMap类对象抛出空指针异常 [英] GoogleMap class object throw Nullpointer Exception

查看:216
本文介绍了为GoogleMap类对象抛出空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我得到空指针错误,在 addMarker()

下面是我的样本code。

  GoogleMap的MYMAP;
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment myMapFragment =(SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
MYMAP = myMapFragment.getMap();
myMap.addMarker(。新的MarkerOptions()位置(经纬度新(0,0))标题(标记)。);
myMap.setMyLocationEnabled(真);
myMap.getUiSettings()setZoomControlsEnabled(真)。
myMap.getUiSettings()setCompassEnabled(真)。
myMap.getUiSettings()setMyLocationButtonEnabled(真)。

在这样一行空指针错误消息

  myMap.addMarker(。新的MarkerOptions()位置(经纬度新(0,0))标题(标记)。);

的main.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    工具:DemoMapActiviy上下文=><的TextView
    机器人:ID =@ + ID / TextView的
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=大文本
    机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?<片段
    机器人:ID =@ + ID /图
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    类=com.google.android.gms.maps.SupportMapFragment/>
< / LinearLayout中>

任何想法,我怎么能解决呢?


解决方案

我通过这个问题了一段时间回来。首先请检查您的片段由片段经理找到。

如果不尝试更换你已经把你的片段到您的视图的方式。如果你想我已经发布了地图的例子 rel=\"nofollow\">另一种方式的例子

更多的问题随时发表评论,我会通过它与您一起:)

修改

你有没有在那里正确的API密钥?如果您无法联系谷歌的服务器,然后有一个很好的机会,那是你的问题。否则,可能是一个明显的问题,请确保您有READ_GSERVICES和INTERNET权限。

请参阅Google地图API V2'无法加载地图。无法联系谷歌服务器

编辑2

好了,所以我被面前偷懒,只是提到你我的项目。如果你获得在项目中的地图连接允许用户从那里开始。

我是在暗示我原来的反应是检查,看看 myMapFragment 为空。如果这是伟大的尝试做沿着以下线的东西:

  FragmentTransaction英尺= getSupportFragmentManager()调用BeginTransaction()。
SupportMapFragment myMapFragment =新SupportMapFragment();
ft.replace(R.id.map,myMapFragment,Constant.TAXI_FRAGMENT_ID);
ft.commit();

在哪里R.id.map是一个空的的FrameLayout

如果 myMapFragment 不为空,然后尝试延长 SupportMapFragment 并运行以下测试:

 公共类MyMapFragment扩展SupportMapFragment {
    @覆盖
    公共无效onActivityCreated()
    {
        如果(的GetMap()== NULL)Log.e(TAG,有一些非常错误的在这里;
    }
}

此外,如果你这样做,确保你膨胀的一个实例 MyMapFragment SupportMapFragment

friends, I am getting NullPointer error ,on addMarker()

Below is my sample code.

GoogleMap myMap;
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment myMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.getUiSettings().setCompassEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);

Error message like NullPointer at line

myMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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"
    android:orientation="vertical"
    tools:context=".DemoMapActiviy" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>

any idea how can i solve it?

解决方案

I went through this issue a while back. First check to see if your fragment is found by the fragment manager.

If not try replacing the way you've put your fragment into your view. If you want an example of another way I've posted a maps example here

Any more questions feel free to comment and I'll work through it with you :)

EDIT

have you got the correct API key in there? If you can't contact Google's servers then there's a good chance that that's your problem. Otherwise may be a manifest issue make sure you have READ_GSERVICES and INTERNET permissions.

See Google Maps API V2 'Failed to Load Map. Could not contact Google Servers'

Edit 2

Ok so I was being lazy before and just referring you to my project. If you were getting a connection to maps in your project lets start from there.

What I was suggesting in my original response was to check and see if myMapFragment was null. If it is great try doing something along the lines of the following:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SupportMapFragment myMapFragment = new SupportMapFragment();
ft.replace(R.id.map, myMapFragment, Constant.TAXI_FRAGMENT_ID);
ft.commit();

Where R.id.map is an empty FrameLayout

If myMapFragment isn't null then try extending SupportMapFragment and running the following test:

public class MyMapFragment extends SupportMapFragment {
    @Override
    public void onActivityCreated()
    {
        if (getMap() == null) Log.e("TAG", "There's something very wrong here";
    }
}

Also if you do this make sure that you are inflating an instance of MyMapFragment not of SupportMapFragment

这篇关于为GoogleMap类对象抛出空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆