【问题标题】:NullPointerException with constructorNullPointerException 与构造函数
【发布时间】:2014-10-18 16:25:59
【问题描述】:

我遇到了 NullPointerException 问题。我上了那堂课。

package com.example.nemaps;

import java.util.ArrayList;

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

public class NMarker {

    public static NMarker getMarker(LatLng position, ArrayList<NMarker> markers){
        for (NMarker marker : markers){
            if (marker.getPosition() == position){
                return marker;
            }
        }
        return null;
    }

    public NMarker(LatLng position, String snippet, String title, int icon,
    int id, GoogleMap map) {
        super();
        this.position = position;
        this.snippet = snippet;
        this.title = title;
        this.icon = icon;
        this.id = id;
        map.addMarker(new MarkerOptions().position(position).snippet(snippet).title(title).icon(BitmapDescriptorFactory.fromResource(icon)));

    }

    public LatLng getPosition() {
        return position;
    }

    public void setPosition(LatLng position) {
        this.position = position;
    }

    public String getSnippet() {
        return snippet;
    }

    public void setSnippet(String snippet) {
        this.snippet = snippet;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    private LatLng position;
    private String snippet;
    private String title;
    private int icon;
    private int id;
}

我在这里使用它

ArrayList<NMarker> markers = new ArrayList<NMarker>();
int i = 0;

NMarker hotel = new NMarker(HOTEL_LOCATION, "5 Stars", "XX Hotel", R.drawable.hotel, i + 1 , map);
i = i+1;
markers.add(hotel);

map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

    @Override
    public void onInfoWindowClick(Marker arg0) {
        // TODO Auto-generated method stub
        Intent ia = new Intent(getActivity().getApplicationContext(), Special.class);

        NMarker marker = getMarker(arg0.getPosition());

        if (marker == null){
            Toast.makeText(getActivity().getApplicationContext(), arg0.getPosition() + "", Toast.LENGTH_LONG).show();
            Toast.makeText(getActivity().getApplicationContext(), "dsadas", Toast.LENGTH_LONG).show();

            Toast.makeText(getActivity().getApplicationContext(), markers.get(0).getPosition() + "", Toast.LENGTH_LONG).show();

        }
        ia.putExtra("page", marker.getId());
        startActivity(ia);
    }
});

在片段上

当我点击它时,它会在此处显示 NullPointerException - ia.putExtra("page", marker.getId());

当我检查时,它是 Toast 相同的位置,但它返回 null。

getMarker 函数 -

public NMarker getMarker(LatLng Position)
{
    for (NMarker marke : markers)
    {
        if (marke.getPosition() == Position)
        {
                return marke;
        }
    }

    return null;                
}

【问题讨论】:

  • 我建议您检查this 问题和那里的第一个答案,并相应地更新您的getMarker 和相关代码。

标签: android


【解决方案1】:

不比较 LatLng 实例,而是比较来自 LatLng 类的经纬度坐标的实际值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2011-04-19
    相关资源
    最近更新 更多