Error when customizing InfoWindowAdapter in Google Map?

0

I have this XML file:

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/ic_carro"
        android:layout_alignBottom="@+id/mmarker_snippet"
        android:layout_alignParentTop="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/marker_titulo"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/mmarker_snippet"
        android:layout_below="@+id/marker_titulo"
        android:layout_alignLeft="@+id/marker_titulo"
        android:layout_alignStart="@+id/marker_titulo" />
</RelativeLayout>

And the code for:

mapa.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

                @Override
                public View getInfoContents(Marker marker) {

                    if (janela == null) {

                        janela = getLayoutInflater().inflate(R.layout.info_window, null);

                        janela.setLayoutParams(new RelativeLayout.LayoutParams(200, RelativeLayout.LayoutParams.WRAP_CONTENT));

                        TextView markerTitulo = (TextView) findViewById(R.id.marker_titulo);
                        markerTitulo.setText(marker.getTitle());

                        TextView markerSnippet = (TextView) findViewById(R.id.mmarker_snippet);
                        markerSnippet.setText(marker.getSnippet());


                    }

                    return janela;
                }

                @Override
                public View getInfoWindow(Marker marker) {

                    return null;
                }

            });

NB: janela refers to a View that I declare in the same class.

But when I run the app, whenever I click the marker, I get a NullPointerException and I do not understand why.

    
asked by anonymous 04.12.2016 / 08:30

1 answer

1

The findViewById () method that you are calling is the object Activity. It finds views that are in the layout passed to the setContentView ( ) .

In the code posted, the views markerTitulo and markerSnippet are in view janela , which was created on the line

janela = getLayoutInflater().inflate(R.layout.info_window, null);

To get reference to these views you should use the findViewById () of the object janela , which is of type View.

instead of

TextView markerTitulo = (TextView) findViewById(R.id.marker_titulo);
markerTitulo.setText(marker.getTitle());

TextView markerSnippet = (TextView) findViewById(R.id.mmarker_snippet);
markerSnippet.setText(marker.getSnippet());

use

TextView markerTitulo = (TextView) janela.findViewById(R.id.marker_titulo);
markerTitulo.setText(marker.getTitle());

TextView markerSnippet = (TextView) janela.findViewById(R.id.mmarker_snippet);
markerSnippet.setText(marker.getSnippet());
    
04.12.2016 / 16:14