user5418227
user5418227

Reputation:

App crashing when open map in dialog

i have custom dialog, i want to show map in the dialog it's working fine but when i show the second time at that time my app is crashing, i stuck in it from many days please guys help to solve it!!!

My code is below

<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();
    }

and in onMapReady method i'm showing marker of current location

My error log is below

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)

Upvotes: 1

Views: 723

Answers (2)

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this: overWrite onDestroy() method: and remove your previous fragment

 @Override
public void onDestroy() {
    super.onDestroy();
    getFragmentManager().beginTransaction().remove(mapfragment_name).commit();
}

if u are using support library use getSupportFragmentManager() instead

Upvotes: 2

RoShan Shan
RoShan Shan

Reputation: 2954

I had the same error with the map in viewpager. Because you use dialog to inflate your map, and you open it again, dialog will inflate your map again (fragment manager will add the same id/tag). YOu don't need to create new dialog multiple time

 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);

You can move codes above to outside method showMapDialog() when you want to show your dialog, just create it 1 time.

Upvotes: 2

Related Questions