Placing SupportMapFragment on DialogFragment

我只是一个虾纸丫 提交于 2020-01-21 18:06:50

问题


I'm trying to add SupportMapFragment on DialogFragment, but it return error inflating class fragment. I cannot figure it out why its returned error inflating class fragment. I think the method I use is correct. I need to extends from DialogFragment because i want show map in dialog.

I have followed this answer, and this answer too, and no one can solve my problem.

Somebody could help me?

Thanks.

Here is my DialogFragment :

import android.support.v4.app.DialogFragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;

/* For widget */
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.Button;

/* Image Downloader Library */
import com.squareup.picasso.Picasso;

/* For string formater */
import com.yai.app.math.support.StringFormater;

/* For object */
import com.yai.app.object.support.Property;
import com.yai.app.object.support.User;

/* For context */
import android.content.Context;

import com.google.android.gms.maps.CameraUpdateFactory;

/* For map */
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class DialogFragmentReadAdvertisement extends DialogFragment{
        private Property propertyData;
    private User userData;
    private Context contextData;

    private GoogleMap mapDetail;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        propertyData = MainActivity.activeProperty;
        userData = MainActivity.activeUser;
        contextData = App.getContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.dialog_fragment_read_advertisement, container, false);

        mapDetail = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_data))
                .getMap();

        mapDetail.addMarker(
                new MarkerOptions().position(propertyData.getLocation())
        );

        CameraPosition cameraPosition = new CameraPosition.Builder()
                                            .target(propertyData.getLocation())
                                            .zoom(19)
                                            .tilt(0)
                                            .bearing(0)
                                            .build();

        mapDetail.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        return view;
    }
}

Here is my xml layout :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- One view for ScrollView -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- Line 13 bellow -->
        <fragment 
          android:id="@+id/map_data"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          class="com.google.android.gms.maps.SupportMapFragment"/>

    </LinearLayout>
</ScrollView>

Here is my LogCat :

07-14 16:28:46.291: E/AndroidRuntime(28121): FATAL EXCEPTION: main
07-14 16:28:46.291: E/AndroidRuntime(28121): android.view.InflateException: Binary XML file line #13: Error inflating class fragment
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at com.yai.properti.tujuh.tujuh.tujuh.DialogFragmentReadAdvertisement.onCreateView(DialogFragmentReadAdvertisement.java:55)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:310)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at com.yai.properti.tujuh.tujuh.tujuh.DialogFragmentReadAdvertisement.onCreateView(DialogFragmentReadAdvertisement.java:55)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.os.Handler.handleCallback(Handler.java:615)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.os.Looper.loop(Looper.java:137)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.app.ActivityThread.main(ActivityThread.java:4895)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at java.lang.reflect.Method.invokeNative(Native Method)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at java.lang.reflect.Method.invoke(Method.java:511)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at dalvik.system.NativeStart.main(Native Method)
07-14 16:28:46.291: E/AndroidRuntime(28121): Caused by: java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f05009e, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
07-14 16:28:46.291: E/AndroidRuntime(28121):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
07-14 16:28:46.291: E/AndroidRuntime(28121):    ... 30 more

回答1:


Try this..

Change this..

mapDetail = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_data))
                .getMap();

to

mapDetail = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_data))
                .getMap();



回答2:


I was facing problem while adding GoogleMapFragment inside DialogFragment and I have solved it as

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (Build.VERSION.SDK_INT < 21) {
        supportMapFragment = (SupportMapFragment) getActivity()
                .getSupportFragmentManager().findFragmentById(R.id.mapDialog);
    } else {
        supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.mapDialog);
    }
    supportMapFragment.getMapAsync(this);
}

Override onDestroy() as

 @Override
public void onDestroy() {
    super.onDestroy();
    if (null != supportMapFragment)
        getActivity().getSupportFragmentManager().beginTransaction()
                .remove(supportMapFragment)
                .commit();
}



回答3:


In case someone else comes along with a similar issue...

To answer your original question, the InflateException (Error inflating class fragment) has occurred because nested fragments - i.e., your SupportMapFragment inside a DialogFragment - may not be inflated from static XML layouts.

See this note from the developer docs:

Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>.
Nested fragments are only supported when added to a fragment programmatically.


So your layout.xml should contain a <FrameLayout.../> for your SupportMapFragment instead of the explicit <fragment.../>, then you'll just add the map fragment via code versus layout inflation.

And since nested fragments must be managed via a FragmentManager from getChildFragmentManager(), you'll want to change the existing getFragmentManager() call when setting up your FragmentTransaction for adding the map fragment programmatically (don't forget to commit!).

For a great example, take another look at the R.layout.mapdialog XML example and associated handling shown in the second SO link you originally included in your question.



来源:https://stackoverflow.com/questions/24733436/placing-supportmapfragment-on-dialogfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!