22

我正在尝试将 aRelativeLayout用作我的标记的自定义InfoWindow,但我NullPointerException每次都得到 ashowInfoWindow被调用(通过点击 aMarker或以编程方式)。我正在使用 Google Maps Android API v2。

例外:

FATAL EXCEPTION: main
java.lang.NullPointerException
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:465)
    at android.view.View.measure(View.java:15172)
    at maps.a.y.i(Unknown Source)
    at maps.a.y.a(Unknown Source)
    at maps.a.w.a(Unknown Source)
    at maps.a.bd.a(Unknown Source)
    at maps.y.bw.b(Unknown Source)
    at maps.y.bw.a(Unknown Source)
    at maps.a.dh.a(Unknown Source)
    at maps.a.n.c(Unknown Source)
    at maps.a.dw.a(Unknown Source)
    at maps.a.bd.c(Unknown Source)
    at maps.a.dq.onSingleTapConfirmed(Unknown Source)
    at maps.e.v.onSingleTapConfirmed(Unknown Source)
    at maps.e.j.handleMessage(Unknown Source)
    ...

我的代码:

*custom_infowindow.xml*

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="5dip"
        android:src="@drawable/icon" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/icon"
        android:ellipsize="end"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:singleLine="true" />

    <TextView
        android:id="@+id/snippet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_toLeftOf="@id/icon"
        android:ellipsize="end"
        android:paddingBottom="10dip"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:singleLine="true" />

</RelativeLayout>

CustomInfoWindowAdapter.java

public class CustomInfoWindowAdapter implements InfoWindowAdapter {

    private View layout;
    private LayoutInflater inflater;

    public CustomInfoWindowAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        ViewHolder holder;

        if (layout == null) {
            layout = inflater.inflate(R.layout.custom_infowindow, null);

            holder = new ViewHolder();
            holder.title = (TextView) layout.findViewById(R.id.title);
            holder.snippet = (TextView) layout.findViewById(R.id.snippet);

            layout.setTag(holder);
        } else {
            holder = (ViewHolder) layout.getTag();
        }

        holder.title.setText(marker.getTitle());
        holder.snippet.setText(marker.getSnippet());

        return layout;
    }

    static class ViewHolder {
        TextView title;
        TextView snippet;
    }

}

有趣的是,如果我LinearLayout改用它,它工作得很好,但如果可能的话,我宁愿只使用一个ViewGroup。当然,我想知道onMeasure投掷NullPointerException.

RelativeLayout编辑:另外,将内部包装aFrameLayout或 aLinearLayout将起作用。当然,将它包裹在另一个里面RelativeLayout不会。

4

3 回答 3

27

我遇到了这个问题,并且能够通过添加来解决它

view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,desiredHeight));

在从 InfoWindowAdapter.getInfoWindow 返回视图之前

于 2013-04-01T19:58:12.093 回答
24

刚刚发现在 android 版本 < 4.4 这会产生一个空指针异常。使用线性布局更改布局将解决问题。

于 2014-05-20T13:19:36.020 回答
4

即使我的 xml 中没有 RelativeLayout,我也遇到了这个问题。事实证明,我的类支持从 RelativeLayout 继承的 XML,即使它是一个 LinearLayout。

它不会对几个不同的设备造成错误,但是当我在我的 S3 上尝试时它崩溃了。

无论如何,只需删除或包装 RelativeLayout 或首先设置布局高度/宽度。

于 2014-07-15T19:41:07.700 回答