其他分享
首页 > 其他分享> > android-在DialogFragment上放置SupportMapFragment

android-在DialogFragment上放置SupportMapFragment

作者:互联网

我正在尝试在DialogFragment上添加SupportMapFragment,但它会返回错误,导致类片段膨胀.我不知道为什么它返回的错误会导致类片段膨胀.我认为我使用的方法是正确的.我需要从DialogFragment扩展,因为我想在对话框中显示地图.

我也遵循这个answerthis answer,而且没人能解决我的问题.

有人可以帮我吗?

谢谢.

这是我的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;
    }
}

这是我的xml布局:

<?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>

这是我的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

解决方法:

尝试这个..

改变这个

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

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

标签:android-maps-v2,android,android-fragments
来源: https://codeday.me/bug/20191013/1909979.html