首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NullPointerException at Marker.setPosition()

NullPointerException at Marker.setPosition()
EN

Stack Overflow用户
提问于 2014-11-17 03:01:45
回答 1查看 140关注 0票数 0

在下面的代码中,我得到了NullPointerException。我知道这意味着我在第228行使用的东西之一是null,而不是有效地址。但我不明白为什么。我错过了什么吗?

代码语言:javascript
复制
public class MainActivity extends ActionBarActivity {

    LatLng xmyLL2;
    private Marker marker2;

    protected void onCreate(Bundle savedInstanceState) {
        marker2 = googleMap.addMarker(new MarkerOptions().
                position(xmyLL2).title("ver2"));
    }

    private class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
            double xd2 = Double.parseDouble(xstr[2]);
            double xd3 = Double.parseDouble(xstr[3]);
            xmyLL2 = new LatLng( xd2, xd3 );
            marker2.setPosition(xmyLL2);  // this is line228
    }
}

line 228: marker2.setPosition(xmyLL2);上,我得到了异常,这是堆栈跟踪:

代码语言:javascript
复制
MainActivity$MyLocationListener.onLocationChanged(Location) line: 228
LocationManager$ListenerTransport._handleMessage(Message) line: 279
LocationManager$ListenerTransport.access$000(LocationManager$ListenerTransport, Message) line: 208
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-17 03:24:14

onLocationChanged()似乎是在onCreate()之前被调用的。

如果您控制了这些,请确保首先调用onCreate。如果您没有控制权,您可以使用if null以避免异常,并希望在onCreate完成其神奇操作后,稍后再次调用它。

将行更改为:

代码语言:javascript
复制
if (null != marker2) marker2.setPosition(xmyLL2);  // this is line228
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26964857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档