gpt4 book ai didi

android - 如何解决android MapView 中的IllegalStateException。 (指定的 child 已经有一个父必须调用 removeView())?

转载 作者:行者123 更新时间:2023-11-29 17:52:48 24 4
gpt4 key购买 nike

我在自定义对话框中显示 map 。第一次单击对话框 map 中的 Showmap 按钮时工作正常。但是当我第二次点击它时它崩溃了。我正在 fragment 中创建对话框。下面是我写的代码。

expandLocation.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View promptView) {
// TODO Auto-generated method stub
LayoutInflater li = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//promptView = new View(getActivity().getApplicationContext());
if (promptView != null) {
ViewGroup parent = (ViewGroup) promptView.getParent();
if (parent != null)

parent.removeView(view);
}

try {
promptView = li.inflate(R.layout.expand_user_location_view, null);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}

if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.

mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}

}

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getActivity());

// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptView);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {

}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

}
});
private void setUpMap() {


latLng = new LatLng(latitude , longitude);
locationzoomWithoutMarker(10, latLng);
}
public void locationzoomWithoutMarker(float zoom,LatLng latLng) {
// TODO Auto-generated method stub

mMap.clear();



LatLng sLatLng = new LatLng(latLng.latitude, latLng.longitude);

mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.addMarker(new MarkerOptions()
.position(sLatLng)
.title("TotalGaz India")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));

mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);


LatLng cameraLatLng = latLng;
float cameraZoom = zoom;

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(cameraLatLng, cameraZoom));
}

这里是我的错误日志

    FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3672)
at android.view.ViewGroup.addView(ViewGroup.java:3543)
at android.view.ViewGroup.addView(ViewGroup.java:3519)
at com.android.internal.app.AlertController.setupView(AlertController.java:402)
at com.android.internal.app.AlertController.installContent(AlertController.java:242)
at android.app.AlertDialog.onCreate(AlertDialog.java:336)
at android.app.Dialog.dispatchOnCreate(Dialog.java:351)
at android.app.Dialog.show(Dialog.java:256)
at com.mobiesprits.yummy_walkins.HomeFragment$8.onClick(HomeFragment.java:467)
at android.view.View.performClick(View.java:4192)
at android.view.View$PerformClick.run(View.java:17248)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4950)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
at dalvik.system.NativeStart.main(Native Method)

我们将不胜感激

最佳答案

首先尝试删除 promtView:

   parent.removeView(promptView);

代替:

   parent.removeView(view);

关于android - 如何解决android MapView 中的IllegalStateException。 (指定的 child 已经有一个父必须调用 removeView())?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759737/

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