6

I'm having problem creating my own CustomInfoWindow. getting exception without know where & why

this is my simple customInfoWindow class

public class CustomInfoWindow implements InfoWindowAdapter {

private LayoutInflater mInflater;

public CustomInfoWindow(LayoutInflater inflater) {
    this.mInflater=inflater;

}


@Override
public View getInfoContents(Marker marker) {
    View popup = mInflater.inflate(R.layout.info_window_layout, null);
    TextView tv=(TextView)popup.findViewById(R.id.title);
    tv.setText(marker.getTitle());
    tv=(TextView)popup.findViewById(R.id.address);
    tv.setText(marker.getSnippet());

    return popup;
}

@Override
public View getInfoWindow(Marker marker) {
    View popup = mInflater.inflate(R.layout.info_window_layout, null);
    TextView tv=(TextView)popup.findViewById(R.id.title);
    tv.setText(marker.getTitle());
    tv=(TextView)popup.findViewById(R.id.address);
    tv.setText(marker.getSnippet());

    return popup;
}

}

I'm setting it here. (MainActivity)

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            Statics.LAT_TLV, Statics.LON_TLV), 13));

    CustomInfoWindow customInfoWindow = new CustomInfoWindow(getLayoutInflater());
    mMap.setInfoWindowAdapter(customInfoWindow);

I didn't implement the onMarkerClick method. the map loads ok with the markers (about 40), but when i click on one of the markers i get:

    01-10 13:15:24.321: E/AndroidRuntime(21162): FATAL EXCEPTION: main
01-10 13:15:24.321: E/AndroidRuntime(21162): java.lang.NullPointerException
01-10 13:15:24.321: E/AndroidRuntime(21162):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at android.view.View.measure(View.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.y.i(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.y.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.w.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.bd.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.y.bw.b(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.y.bw.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.dh.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.n.c(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.dw.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.bd.c(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.dq.onSingleTapConfirmed(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.e.v.onSingleTapConfirmed(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.e.j.handleMessage(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at android.os.Handler.dispatchMessage(Handler.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at android.os.Looper.loop(Looper.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at android.app.ActivityThread.main(ActivityThread.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at java.lang.reflect.Method.invoke(Method.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at dalvik.system.NativeStart.main(Native Method)

would appreciate if someone can help me with this, and if there's a very simple example of how to create you own info view it would be wonderful. thanks very much. Udi

Udi Oshi
  • 6,787
  • 7
  • 47
  • 65

1 Answers1

13

You should not be overriding both getInfoWindow() and getInfoContents(), at least where getInfoWindow() can never return null. Your getInfoContents() will never be used.

If I had to guess, your problem lies in your layout file, but that is just a guess.

Here is a sample InfoWindowAdapter:

class PopupAdapter implements InfoWindowAdapter {
  LayoutInflater inflater=null;

  PopupAdapter(LayoutInflater inflater) {
    this.inflater=inflater;
  }

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

  @Override
  public View getInfoContents(Marker marker) {
    View popup=inflater.inflate(R.layout.popup, null);

    TextView tv=(TextView)popup.findViewById(R.id.title);

    tv.setText(marker.getTitle());
    tv=(TextView)popup.findViewById(R.id.snippet);
    tv.setText(marker.getSnippet());

    return(popup);
  }
}

Using this layout file for the InfoWindow:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:padding="2dip"
        android:src="@drawable/ic_launcher"
        android:contentDescription="@string/icon"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/snippet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"/>
    </LinearLayout>

</LinearLayout>

The complete sample application can be found at: https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Popups

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks very much. works great now. I don't really need to implement the getInfoWindow, just returning null. thanks alot – Udi Oshi Jan 10 '13 at 13:04
  • This seems to be great... but how to call this from MainActivity and apply this to every marker of the map? – smartmouse Jan 04 '15 at 16:05
  • @smartmouse: This does apply to every marker of the map, and the `PopupAdapter` is applied in `MainActivity`, as you can see when examining the project linked to in the answer. – CommonsWare Jan 04 '15 at 16:31
  • Do you know how to show title and snippet for every marker in a cluster? – smartmouse Jan 04 '15 at 16:55
  • @smartmouse: That I don't know, and it would depend a bit on what you are using for the clustering, I imagine. – CommonsWare Jan 04 '15 at 17:00
  • I'm using this: https://developers.google.com/maps/documentation/android/utility/marker-clustering – smartmouse Jan 04 '15 at 20:37
  • In this answer, in PopupAdapter.java, when i use marker.getTitle() and marker.getSnippet(), they are empty. How to validate them? – smartmouse Jan 04 '15 at 20:40
  • @smartmouse: They should not be empty in my sample. If you are running into problems with your own code, I recommend that you open a fresh Stack Overflow question, rather than commenting here. – CommonsWare Jan 04 '15 at 20:43
  • Thank you for your help, but i have already created a question, can you please reply me there? http://stackoverflow.com/questions/27747435/how-to-set-details-for-infowindow-using-clusteritem – smartmouse Jan 04 '15 at 20:49