gpt4 book ai didi

android - 使用 RelativeLayout 作为自定义 InfoWindow 时出现 NullPointerException

转载 作者:IT老高 更新时间:2023-10-28 22:24:20 27 4
gpt4 key购买 nike

我正在尝试使用 RelativeLayout 作为我的标记的自定义 InfoWindow,但是每次 时我都会得到一个 NullPointerException调用 showInfoWindow(通过点击 Marker 或以编程方式)。我正在使用 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)
...

我的代码:

*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;
}

}

有趣的是,如果我使用 LinearLayout 代替它工作得很好,但如果可能的话,我宁愿只使用一个 ViewGroup。当然,我想知道抛出 NullPointerExceptiononMeasure 会发生什么。

编辑:另外,将 RelativeLayout 包装在 FrameLayoutLinearLayout 内也可以。当然,将其包装在另一个 RelativeLayout 中是不会的。

最佳答案

我遇到了这个问题,并且能够通过添加来解决它

view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,desiredHeight));

在从 InfoWindowAdapter.getInfoWindow 返回之前到 View

关于android - 使用 RelativeLayout 作为自定义 InfoWindow 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14023588/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com