NullPointerException podczas używania RelativeLayout jako custom InfoWindow

Próbuję użyć RelativeLayout jako niestandardowego InfoWindow dla moich markerów, ale a dostaję NullPointerException za każdym razem, gdy showInfoWindow jest wywoływany (poprzez stuknięcie Marker lub programowo). Używam Google Maps Android API v2.

Wyjątek:

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)
    ...

Mój kod:

*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;
    }

}

Zabawne jest to, że jeśli używam LinearLayout zamiast tego działa to dobrze, ale wolałbym użyć tylko jednego ViewGroup jeśli możliwe. I oczywiście chciałbym wiedzieć, co się stanie z tym onMeasure rzucaniem NullPointerException.

EDIT: również zawijanie RelativeLayout wewnątrz FrameLayout lub LinearLayout będzie działać. Owijanie go w inny RelativeLayout Oczywiście tego nie zrobi.

Author: JJD, 2012-12-24

3 answers

Miałem ten problem i mogłem go naprawić dodając

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

Do widoku przed zwróceniem go z InfoWindowAdapter.getInfoWindow

 27
Author: ootinii,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-01 19:58:12

Właśnie się dowiedziałem, że w wersji Androida

 24
Author: Pizza,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-20 13:19:36

Doświadczyłem tego problemu nawet wtedy, gdy nie miałem RelativeLayout w moim xml. Okazuje się, że moja klasa wspiera XML odziedziczony po RelativeLayout, mimo że był to LinearLayout.

To nie spowodowało błąd dla kilku różnych urządzeń, ale kiedy próbowałem na moim S3 to się rozbił.

Tak czy inaczej, po prostu usuń lub zawiń RelativeLayout lub ustaw najpierw wysokość/szerokość układu.

 4
Author: skr1p7k1dd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-15 19:41:07