无法在android中的警报对话框中执行片段事务

时间:2017-06-20 10:32:18

标签: android google-maps android-fragments alertdialog android-dialogfragment

我有一个片段,其中我有谷歌地图。我正在尝试将此片段保留在警报对话框中。但它不接受任何方式。

首先,它不允许我在对话框中允许v4片段。我甚至试图通过构造函数传递该v4片段对象。

我得到一个错误,说该活动不应该有一个构造函数!多么令人沮丧!!

这是我的片段代码:

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

    <com.google.android.gms.maps.MapView
        android:id="@+id/google_map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </com.google.android.gms.maps.MapView>
</FrameLayout>

我用于片段布局的代码是这个

package com.acs.AlertAndProgressDialogs;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.google.android.gms.maps.MapView;
import com.acs.AfterLogin.MapDialogFragment;
import com.acs.R;


public class GoogleMapLocationPicker extends DialogFragment {

    View layout;
    MapView mapView;
    MapDialogFragment googleMapFragment;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        layout = inflater.inflate(R.layout.google_map_frag_dialog, null);
        googleMapFragment=new MapDialogFragment();
        FragmentManager manager=getActivity().getFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.replace(R.id.map_replacable_frag,googleMapFragment);



    /* This is where I'm ending with error. It is saying it requires a fragment 
    object but it found object of type MapDialogFragment. 
    I have even tried using getChildFragmentManager() even that is also not 
    working at here.*/


    }
}

我用于警告对话框的代码是:

{{1}}

提前多多感谢...

2 个答案:

答案 0 :(得分:1)

你试图实现这个错误的方式。您无法用对话框片段替换v4片段。 首先,不建议在对话框片段中使用谷歌地图,但如果仍然想以这种方式使用谷歌地图,你应该去自定义对话框片段。

试试这个:

public class CustomDialogFragment extends DialogFragment {
    /** The system calls this to get the DialogFragment's layout, regardless
        of whether it's being displayed as a dialog or an embedded fragment. */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        return inflater.inflate(R.layout.yourmaplayout, container, false);
    }

    /** The system calls this only when creating the layout in a dialog. */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // The only reason you might override this method when using onCreateView() is
        // to modify any dialog characteristics. For example, the dialog includes a
        // title by default, but your custom layout might not need it. So here you can
        // remove the dialog title, but you must call the superclass to get the Dialog.
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
}

然后显示对话框:

FragmentManager fragmentManager = getSupportFragmentManager();
CustomDialogFragment newFragment = new CustomDialogFragment();
newFragment.show(fragmentManager, "dialog");

答案 1 :(得分:0)

我找到了解决问题的方法。经过测试,它正在运行。

以下是代码:

public class GoogleMapLocationPicker extends DialogFragment implements OnMapReadyCallback {

    View layout;
    MapView mapView;
    GoogleMap googleMap;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        layout = inflater.inflate(R.layout.google_map_frag_dialog, null);
        mapView = (MapView) layout.findViewById(R.id.google_map_view);
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setView(layout);
        return builder.create();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mapView.onCreate(null);
        mapView.onResume();
        mapView.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Toast.makeText(getActivity(), "Map ready...", Toast.LENGTH_SHORT).show();
        MapsInitializer.initialize(getActivity());
        this.googleMap=googleMap;
        googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        googleMap.addMarker(new MarkerOptions().position(new LatLng(16,80)));
    }
}

此对话框随附一张地图。希望这会有所帮助:)