我有自定义对话框,我想在对话框中显示地图它工作正常但是当我第二次显示我的应用程序崩溃时,我坚持了很多天请帮助解决它!!!
我的代码在
下面<fragment
android:id="@+id/mGoogleMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity.java
private void showMapDialog() {
mDialog = new Dialog(MainActivity.this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mDialog.setContentView(R.layout.dialog_map_view);
mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mIsDialogueClick = false;
}
});
mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mIsDialogueClick = false;
}
});
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mGoogleMap);
if (mMap == null) {
mapFragment.getMapAsync(this);
}
if (mDialog != null && !mDialog.isShowing()) {
mDialog.show();
mIsDialogueClick = true;
}
// mDialog.show();
}
并在onMapReady方法中显示当前位置的标记
我的错误日志在
之下02-20 15:35:41.356 23874-23874/com.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 23874
android.view.InflateException: Binary XML file line #43: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:407)
at android.app.Dialog.setContentView(Dialog.java:490)
at com.example.forms.MainActivity.showMapDialog(MainActivity.java:614)
at com.example.forms.MainActivity.access$600(MainActivity.java:49)
at com.example.forms.MainActivity$3.onClick(MainActivity.java:274)
at android.view.View.performClick(View.java:4788)
at android.view.View$PerformClick.run(View.java:19923)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5382)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:928)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:723)
答案 0 :(得分:2)
试试这个:overWrite onDestroy()
方法:并删除之前的片段
@Override
public void onDestroy() {
super.onDestroy();
getFragmentManager().beginTransaction().remove(mapfragment_name).commit();
}
如果您使用支持库,则使用getSupportFragmentManager()
代替
答案 1 :(得分:1)
我在viewpager中遇到了与地图相同的错误。因为您使用对话框来膨胀您的地图,并再次打开它,对话框将再次膨胀您的地图(片段管理器将添加相同的ID /标记)。你不需要多次创建新的对话框
mDialog = new Dialog(MainActivity.this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mDialog.setContentView(R.layout.dialog_map_view);
当您想要显示对话框时,可以将上面的代码移到外部方法showMapDialog()
,只需创建一次。