Try to remove the promtView
first:
parent.removeView(promptView);
instead of:
parent.removeView(view);
質問
I am showing map in custom dialog. first time when i click Showmap button in the dialog map is working fine. but when i click it second time it s crashing. i am creating dialog in fragment. below is the code i have written.
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));
}
and my error log here
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)
any help will be appriciated
解決
Try to remove the promtView
first:
parent.removeView(promptView);
instead of:
parent.removeView(view);