Error when customizing Infowindowadapter on Google Map?

Asked

Viewed 39 times

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 I declared in the same class.

But when I run the app, every time I click on the marker, I get a Nullpointerexception and I don’t understand why.

1 answer

1


The method findViewById() is from the Activity object.
He finds views that are in the layout passed to the method setContentView().

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

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

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

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());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.