问题描述
我试图为我的标记使用 RelativeLayout
作为自定义 InfoWindow
,但是我得到 NullPointerException
每次调用 showInfoWindow
(通过点击 Marker
或以编程方式)。我使用的是Google Maps Android API v2。
例外情况:
FATAL EXCEPTION:main
android.widget.RelativeLayout.onMeasure(RelativeLayout.java:465)$ b $ java.lang.NullPointerException
$ b在android.view.View.measure(View.java:15172)
在maps.ayi(未知源)
在maps.aya(未知源)
在maps.awa(未知源)
在maps.a.bd.a(未知源)
在maps.y.bw.b(未知源)
在maps.y.bw.a(未知源)
at maps.a.dh.a(未知来源)
at maps.anc(未知来源)
at maps.a.dw.a(未知来源)
at地图.a.bd.c(未知来源)
at maps.a.dq.onSingleTapConfirmed(未知来源)
at maps.evonSingleTapConfirmed(未知来源)
at maps.ejhandleMessage(未知来源)
...
我的代码
* cus tom_infowindow.xml *
<?xml version =1.0encoding =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 {
$ b $ private view View layout;
私人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持有者;
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(持有人);
} else {
holder =(ViewHolder)layout.getTag();
}
holder.title.setText(marker.getTitle());
holder.snippet.setText(marker.getSnippet());
返回布局;
}
static class ViewHolder {
TextView title;
TextView snippet;
}
}
有趣的是,如果我使用 LinearLayout
相反,它可以正常工作,但如果可能的话,我宁愿只使用一个 ViewGroup
。当然,我想知道 onMeasure
抛出一个 NullPointerException
。
编辑:另外,在 FrameLayout
或<$ c $内包装 RelativeLayout
c> LinearLayout 可以工作。将它包装在另一个 RelativeLayout
当然不会。
我有这个问题,并能够通过添加
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,desiredHeight)) ;
到InfoWindowAdapter.getInfoWindow返回之前的视图
I'm trying to use a RelativeLayout
as custom InfoWindow
for my markers, but a I get a NullPointerException
every time showInfoWindow
is called (via tapping on a Marker
or programmatically). I'm using Google Maps Android API v2.
Exception:
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)
...
My code:
*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;
}
}
Funny thing is that if I use LinearLayout
instead it works just fine, but I'd rather use only one ViewGroup
if possible. And of course I would like to know what happens with that onMeasure
throwing a NullPointerException
.
EDIT: Also, wrapping the RelativeLayout
inside a FrameLayout
or a LinearLayout
will work. Wrapping it inside another RelativeLayout
won't, of course.
I had this problem and was able to fix it by adding
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,desiredHeight));
to the view before returning it from InfoWindowAdapter.getInfoWindow
这篇关于将RelativeLayout用作自定义InfoWindow时的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!