我正在使用Android谷歌地图地图上有不同的引脚和nam和关闭按钮和移动按钮.popup在地图上显示但是当点击另一个引脚时给出例外情况: -
10-06 22:17:17.080: E/AndroidRuntime(25156): FATAL EXCEPTION: main
10-06 22:17:17.080: E/AndroidRuntime(25156): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.addViewInner(ViewGroup.java:3011)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.addView(ViewGroup.java:2900)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.addView(ViewGroup.java:2880)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.appstudioz.bidnear.map.BidNearItemizedOverlay.onTap(BidNearItemizedOverlay.java:85)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:356)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.google.android.maps.MapView.onTouchEvent(MapView.java:683)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.View.dispatchTouchEvent(View.java:4609)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1554)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1320)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1732)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1267)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.app.Activity.dispatchTouchEvent(Activity.java:2315)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1705)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1333)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1732)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1267)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.app.Activity.dispatchTouchEvent(Activity.java:2315)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1705)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.View.dispatchPointerEvent(View.java:4677)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2394)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.view.ViewRoot.handleMessage(ViewRoot.java:2054)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.os.Looper.loop(Looper.java:132)
10-06 22:17:17.080: E/AndroidRuntime(25156): at android.app.ActivityThread.main(ActivityThread.java:4123)
10-06 22:17:17.080: E/AndroidRuntime(25156): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 22:17:17.080: E/AndroidRuntime(25156): at java.lang.reflect.Method.invoke(Method.java:491)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-06 22:17:17.080: E/AndroidRuntime(25156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-06 22:17:17.080: E/AndroidRuntime(25156): at dalvik.system.NativeStart.main(Native Method)
我使用的代码如下: - 我的webservice给出了lat,long并在地图上显示的对象数量: -
MapController objmapcontroller;
GeoPoint objgeopointp;
double lat, lng;
try {
lat = Double.parseDouble(latitude);
lng = Double.parseDouble(longitude);
} catch (Exception e) {
e.printStackTrace();
lat = lng = 0;
}
objgeopointp = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
objmapcontroller = objmapView.getController();
// ---Add a location marker---
// create and add an OverlayItem to the MyItemizedOverlay list
OverlayItem overlayitem = new OverlayItem(objgeopointp, title, snippet);
itemizedoverlay.addOverlay(overlayitem);
objmapcontroller.animateTo(objgeopointp);
objmapcontroller.setZoom(16);
// add the overlays to the map
mapOverlays.add(itemizedoverlay);
objmapView.invalidate();
我的覆盖类就像那个类一样,Ontap方法整个类就像
一样private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
private MapView objmapview;
private View popUp;
public BidNearItemizedOverlay(Drawable defaultMarker, Context ctx,MapView objmapview,View popUp) {
super(boundCenterBottom(defaultMarker));
mContext =ctx;
this.objmapview=objmapview;
this.popUp=popUp;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
Log.e("------size--",""+mOverlays.size());
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item=getItem(index);
GeoPoint geo=item.getPoint();
MapView.LayoutParams mapParams = new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
geo,
MapView.LayoutParams.BOTTOM_CENTER);
TextView objtxt = (TextView)popUp.findViewById(R.id.textup);
TextView objtxt2 = (TextView)popUp.findViewById(R.id.text2);
objtxt.setText(item.getTitle());
objtxt2.setText(item.getSnippet());
ImageView objimg = (ImageView)popUp.findViewById(R.id.bluebutton);
ImageButton objbtn = (ImageButton)popUp.findViewById(R.id.closebutton);
objbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("aclosehjdfd");
}
});
objimg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("hohodshfsdh");
}
});
objmapview.addView(popUp, mapParams);
return true;
}
请有人建议我如何才能提前实现这个目标..
答案 0 :(得分:2)
您发送给BidNearItemizedOverlay的View popUp看起来已经有了父级。当你调用addView(popUp,mapParams)时,你试图给它第二个父。这是不允许的。如果您确实想要使用它,则必须先将其与父项分离。最简单的解决方案可能是根据popUp中的属性创建一个新视图,而不是使用该实际视图。
或尝试:
((ViewGroup)popUp.getParent()).removeView(popUp);