我无法在Mapbox的自定义信息窗口中为视图添加视图. (使用图像设置getinfowindow) [英] I can't inflate a view in a custom infowindow in Mapbox. (Setting getinfowindow with images)

查看:104
本文介绍了我无法在Mapbox的自定义信息窗口中为视图添加视图. (使用图像设置getinfowindow)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mapbox和Android Studio IDE为Android制作地图指南,但是在处理自定义信息窗口时遇到了困难.

我想给信息窗口充气(单击标记后的那个),但到目前为止,我想为此使用XML进行简单的自定义(我愿意接受建议,但我仍然需要添加其他图像每个标记.).我只是在代码本身中使用自定义进行测试,但是我想为该目的添加XML.

下面的图像显示了我正在为视图开发的原型,下一个图像显示了我正在从测试中实际获得的内容(代码在getInfoWindow函数内部),我正在使用以下代码进行开发:

XML原型(此处为:ID转换类型)

我得到了什么

下面是我正在使用的部分地图代码,下一个代码显示了XML代码.

主代码(Mapa.Java):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MapboxAccountManager.start(this,getString(R.string.access_token));

    setContentView(R.layout.activity_main);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {

            IconFactory iconFactory = IconFactory.getInstance(Mapa.this);
            Drawable iconDrawable = ContextCompat.getDrawable(Mapa.this, R.drawable.infoicon);
            Icon icon = iconFactory.fromDrawable(iconDrawable);

            mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(@NonNull Marker marker) {

                    View v = getLayoutInflater().inflate(R.layout.janela_infowindow, null);

                    TextView titulo = (TextView) v.findViewById(R.id.titulo);
                    TextView desc = (TextView) v.findViewById(R.id.descricao);
                    titulo.setText(marker.getTitle());
                    desc.setText(marker.getSnippet());

                    titulo.setTextColor(Color.WHITE);
                    desc.setTextColor(Color.WHITE);

                    v.setBackgroundColor(Color.rgb(27,77,62));
                    v.setPadding(10, 10, 10, 10);
                    return v;
                }
            });

            mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(-15.76363, -47.86949))
                    .title("ICC Centro")
                    .snippet(getString(R.string.desc_ICC_Centro))
                    .icon(icon));

            mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(-15.7629936, -47.86717415))
                    .title("Reitoria")
                    .snippet(getString(R.string.desc_Reitoria))
                    .icon(icon));

            mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(-15.76196106, -47.87008166))
                    .title("FAU")
                    .snippet(getString(R.string.desc_FAU))
                    .icon(icon));
        }
    });
}

XML代码(janela_infowindow.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="200dp"
    android:layout_height="270dp"
    android:weightSum="1">

<ImageView
    android:layout_width="200dp"
    android:layout_height="0dp"
    android:id="@+id/imagem"
    android:adjustViewBounds="true"
    android:layout_gravity="end"
    android:contentDescription="@string/nome_local"
    android:layout_weight="0.11" />

<TextView
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="-30dp"
    android:gravity="center"
    android:textAlignment="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/titulo"
    android:typeface="serif"
    android:textStyle="bold"/>

<TextView
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/descricao" />

我不确定这是否正确,但是每次我要设置高度,宽度或图像时,都会出现此错误消息(此消息错误是在设置高度和宽度后给出的宽度):

FATAL EXCEPTION: main
Process: afinal.projeto.unb.guiadoaluno, PID: 8979
java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.height' on a null object reference
at afinal.projeto.unb.guiadoaluno.Mapa$1$1.getInfoWindow(Mapa.java:87)
at com.mapbox.mapboxsdk.annotations.Marker.showInfoWindow(Marker.java:165)
at com.mapbox.mapboxsdk.maps.MapboxMap.selectMarker(MapboxMap.java:1295)
at com.mapbox.mapboxsdk.maps.MapView$GestureListener.onSingleTapConfirmed(MapView.java:1792)
at android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:280)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5940)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

好的,所以我对无法将图像放置在getInfoWindow内感到沮丧,这与设置图像的方式无关,它始终是相同的错误消息.我知道可以设置图像,但是仅当您在java文件中创建对象时,如果要从膨胀的XML更改图像,就不可能做同样的事情.无论我声明,设置或使用java文件中的图像如何,这都是我得到的消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

如果有人知道如何解决该问题,我相信这将在Stack Overflow中结束我的问题.

解决方案

因此,我发现了问题.恰如Cammace所说的那样,问题出在XML中,不知何故存在诸如android:weightSum ="1"之类的属性以及具有wrap_content的内容.

感谢您的帮助,Mapbox团队!

I am doing a map guide for Android using Mapbox and Android Studio IDE, but I am having a hard time dealing with the custom infowindow.

I want to inflate the infowindow (the one after I click on a marker) but as of yet I want to use an XML for that for an easy customization (I am open for suggestions, I still need to add a different image for each of the markers.). I am using a customization in the code itself just for testing, but I want to inflate an XML for that purporse.

The following images below show the prototype I am developing for the view and the next image shows what I am actually getting from the test (The code is inside the getInfoWindow function) I am developing with the code below:

Prototype in XML (Where as: ID-Translation-Type)

What I am getting

Here follows the part of the map code I am using, and the next code shows the XML code.

Main code (Mapa.Java):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MapboxAccountManager.start(this,getString(R.string.access_token));

    setContentView(R.layout.activity_main);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {

            IconFactory iconFactory = IconFactory.getInstance(Mapa.this);
            Drawable iconDrawable = ContextCompat.getDrawable(Mapa.this, R.drawable.infoicon);
            Icon icon = iconFactory.fromDrawable(iconDrawable);

            mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(@NonNull Marker marker) {

                    View v = getLayoutInflater().inflate(R.layout.janela_infowindow, null);

                    TextView titulo = (TextView) v.findViewById(R.id.titulo);
                    TextView desc = (TextView) v.findViewById(R.id.descricao);
                    titulo.setText(marker.getTitle());
                    desc.setText(marker.getSnippet());

                    titulo.setTextColor(Color.WHITE);
                    desc.setTextColor(Color.WHITE);

                    v.setBackgroundColor(Color.rgb(27,77,62));
                    v.setPadding(10, 10, 10, 10);
                    return v;
                }
            });

            mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(-15.76363, -47.86949))
                    .title("ICC Centro")
                    .snippet(getString(R.string.desc_ICC_Centro))
                    .icon(icon));

            mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(-15.7629936, -47.86717415))
                    .title("Reitoria")
                    .snippet(getString(R.string.desc_Reitoria))
                    .icon(icon));

            mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(-15.76196106, -47.87008166))
                    .title("FAU")
                    .snippet(getString(R.string.desc_FAU))
                    .icon(icon));
        }
    });
}

XML code (janela_infowindow.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="200dp"
    android:layout_height="270dp"
    android:weightSum="1">

<ImageView
    android:layout_width="200dp"
    android:layout_height="0dp"
    android:id="@+id/imagem"
    android:adjustViewBounds="true"
    android:layout_gravity="end"
    android:contentDescription="@string/nome_local"
    android:layout_weight="0.11" />

<TextView
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="-30dp"
    android:gravity="center"
    android:textAlignment="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/titulo"
    android:typeface="serif"
    android:textStyle="bold"/>

<TextView
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/descricao" />

EDIT: I am not sure if this is correct, but every time I am going to set the height, width or set the image, it gives me this error message (This message error was given after setting the height and width):

FATAL EXCEPTION: main
Process: afinal.projeto.unb.guiadoaluno, PID: 8979
java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.height' on a null object reference
at afinal.projeto.unb.guiadoaluno.Mapa$1$1.getInfoWindow(Mapa.java:87)
at com.mapbox.mapboxsdk.annotations.Marker.showInfoWindow(Marker.java:165)
at com.mapbox.mapboxsdk.maps.MapboxMap.selectMarker(MapboxMap.java:1295)
at com.mapbox.mapboxsdk.maps.MapView$GestureListener.onSingleTapConfirmed(MapView.java:1792)
at android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:280)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5940)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

EDIT 2: Ok, so I am getting frustrated with the fact that I can't put an image inside the getInfoWindow, it does not matter how I set the images, it's always the same error message. I know that it's possible to set images, but that is only when you create the object inside the java file, it's not possible to do the same when you want to change images from an inflated XML. This is the message I get no matter what I declare, set or use the image inside the java file:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

If someone knows how to solve that, I do believe that would close my question here in Stack Overflow.

解决方案

So, I have found the problem. It was exactly as Cammace said, the problem was in the XML which somehow there were properties such as android:weightSum="1" and the contents that had wrap_content.

Thanks for the help, Mapbox Team!

这篇关于我无法在Mapbox的自定义信息窗口中为视图添加视图. (使用图像设置getinfowindow)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆