Android 在google map v2中创建CustomInfoWindow
我在创建自己的CustomInfoWindow时遇到问题。在不知道发生在何处和为什么的情况下获取异常 这是我简单的customInfoWindow类Android 在google map v2中创建CustomInfoWindow,android,google-maps-android-api-2,infoview,Android,Google Maps Android Api 2,Infoview,我在创建自己的CustomInfoWindow时遇到问题。在不知道发生在何处和为什么的情况下获取异常 这是我简单的customInfoWindow类 public class CustomInfoWindow implements InfoWindowAdapter { private LayoutInflater mInflater; public CustomInfoWindow(LayoutInflater inflater) { this.mInflater=inflater
public class CustomInfoWindow implements InfoWindowAdapter {
private LayoutInflater mInflater;
public CustomInfoWindow(LayoutInflater inflater) {
this.mInflater=inflater;
}
@Override
public View getInfoContents(Marker marker) {
View popup = mInflater.inflate(R.layout.info_window_layout, null);
TextView tv=(TextView)popup.findViewById(R.id.title);
tv.setText(marker.getTitle());
tv=(TextView)popup.findViewById(R.id.address);
tv.setText(marker.getSnippet());
return popup;
}
@Override
public View getInfoWindow(Marker marker) {
View popup = mInflater.inflate(R.layout.info_window_layout, null);
TextView tv=(TextView)popup.findViewById(R.id.title);
tv.setText(marker.getTitle());
tv=(TextView)popup.findViewById(R.id.address);
tv.setText(marker.getSnippet());
return popup;
}
}
我在这里设置。(主要活动)
我没有实现onMarkerClick方法。
地图可以加载标记(大约40个),但当我单击其中一个标记时,我会得到:
01-10 13:15:24.321: E/AndroidRuntime(21162): FATAL EXCEPTION: main
01-10 13:15:24.321: E/AndroidRuntime(21162): java.lang.NullPointerException
01-10 13:15:24.321: E/AndroidRuntime(21162): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java)
01-10 13:15:24.321: E/AndroidRuntime(21162): at android.view.View.measure(View.java)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.y.i(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.y.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.w.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.bd.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.y.bw.b(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.y.bw.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.dh.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.n.c(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.dw.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.bd.c(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.dq.onSingleTapConfirmed(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.e.v.onSingleTapConfirmed(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.e.j.handleMessage(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162): at android.os.Handler.dispatchMessage(Handler.java)
01-10 13:15:24.321: E/AndroidRuntime(21162): at android.os.Looper.loop(Looper.java)
01-10 13:15:24.321: E/AndroidRuntime(21162): at android.app.ActivityThread.main(ActivityThread.java)
01-10 13:15:24.321: E/AndroidRuntime(21162): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 13:15:24.321: E/AndroidRuntime(21162): at java.lang.reflect.Method.invoke(Method.java)
01-10 13:15:24.321: E/AndroidRuntime(21162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
01-10 13:15:24.321: E/AndroidRuntime(21162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
01-10 13:15:24.321: E/AndroidRuntime(21162): at dalvik.system.NativeStart.main(Native Method)
如果有人能帮我,我将不胜感激。如果有一个非常简单的例子,说明如何创建自己的信息视图,那将非常棒。非常感谢。Udi您不应该同时覆盖
getInfoWindow()
和getInfoContents()
,至少在getInfoWindow()
永远不能返回null的地方。您的getInfoContents()
将永远不会被使用
如果我不得不猜测,你的问题在于你的布局文件,但这只是一个猜测
下面是一个示例InfoWindowAdapter
:
class PopupAdapter implements InfoWindowAdapter {
LayoutInflater inflater=null;
PopupAdapter(LayoutInflater inflater) {
this.inflater=inflater;
}
@Override
public View getInfoWindow(Marker marker) {
return(null);
}
@Override
public View getInfoContents(Marker marker) {
View popup=inflater.inflate(R.layout.popup, null);
TextView tv=(TextView)popup.findViewById(R.id.title);
tv.setText(marker.getTitle());
tv=(TextView)popup.findViewById(R.id.snippet);
tv.setText(marker.getSnippet());
return(popup);
}
}
将此布局文件用于信息窗口
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="2dip"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/icon"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>
完整的示例应用程序可在以下网址找到:非常感谢。现在效果很好。我真的不需要实现getInfoWindow,只需要返回null。非常感谢这看起来很棒。。。但是如何从MainActivity调用它并将其应用于地图的每个标记?@smartmouse:这确实应用于地图的每个标记,并且PopupAdapter
应用于MainActivity
,正如您在检查答案中链接到的项目时所看到的那样。您知道如何显示群集中每个标记的标题和代码段吗?@smartmouse:我不知道,我想这将取决于您在群集中使用的内容。我有完全相同的错误,如果我使用getInfoContents,一切都很好,但如果我尝试getInfoWindows,应用程序崩溃。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="2dip"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/icon"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>