使用com.google.android.gms.maps.SupportMapFragment,android?的另一个片段重复id,tag null或parent id?

时间:2016-04-28 12:23:50

标签: dictionary dialog fragment layout-inflater

我有自己的布局对话框,其中包含一个地图片段。

public class DialogEditCustomer extends Dialog implements View.OnClickListener, OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private DialogEditCustomer.OnButtonClick onButtonClick;
private EditText etOwnerName, etOwnerNumber;
private TextView tvLatLong;
private Button buttonSave, buttonCancel;
private GoogleMap mMap;
private String ownerName, ownerNumber, latitude, longitude;
private FragmentManager fragmentManager;
private Context context;
private Location locationLatLong;
private LatLng markerLatLong;
SupportMapFragment mapFragment;

//initialize the dialog
public DialogEditCustomer(Context context, String ownerName, String ownerNumber, String latitude, String longitude, FragmentManager fragmentManager) {
    super(context);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.setContentView(R.layout.dialog_edit_customer);
    this.ownerName = ownerName;
    this.ownerNumber = ownerNumber;
    this.latitude = latitude;
    this.longitude = longitude;
    this.fragmentManager = fragmentManager;
    this.context = context;
    initViews();
    if (context instanceof DialogEditCustomer.OnButtonClick) {
        onButtonClick = (DialogEditCustomer.OnButtonClick) context;
    }
}

//initialise views
private void initViews() {
    tvLatLong = (TextView) findViewById(R.id.textViewLatLong);
    etOwnerName = (EditText) findViewById(R.id.editTextOwnerName);
    etOwnerNumber = (EditText) findViewById(R.id.editTextOwnerNumber);
    buttonSave = (Button) findViewById(R.id.buttonSave);
    buttonCancel = (Button) findViewById(R.id.buttonCancel);
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }
    mapFragment = (SupportMapFragment) fragmentManager
            .findFragmentById(R.id.mapDialog);

    buttonSave.setOnClickListener(this);
    buttonCancel.setOnClickListener(this);
    if (ownerName != null) {
        etOwnerName.setText(ownerName);
    }
    if (ownerNumber != null) {
        etOwnerNumber.setText(ownerNumber);
    }
    if (latitude != null && longitude != null) {
        tvLatLong.setText(latitude + "," + longitude);
    }

}

GoogleApiClient mGoogleApiClient;

@Override
public void onClick(View v) {
    if (v == buttonSave) {
        ownerName = etOwnerName.getText().toString();
        ownerNumber = etOwnerNumber.getText().toString();
        if (markerLatLong == null) {
            latitude = "" + locationLatLong.getLatitude();
            longitude = "" + locationLatLong.getLongitude();
        } else {
            latitude = "" + markerLatLong.latitude;
            longitude = "" + markerLatLong.longitude;
        }
        onButtonClick.onSaveButtonClick(ownerName, ownerNumber, latitude, longitude);
    }
    if (v == buttonCancel) {
        onButtonClick.onCancelButtonClick();
    }
}

// implement for button clicks in activity and fragment
public interface OnButtonClick {
    void onSaveButtonClick(String ownerName, String ownerNumber, String latitude, String longitude);

    void onCancelButtonClick();
}

//only to be used in case of fragment
public void setListener(DialogEditCustomer.OnButtonClick onButtonClick) {
    this.onButtonClick = onButtonClick;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    try {
        mMap.setMyLocationEnabled(true);
    } catch (SecurityException e) {
        e.printStackTrace();
    }

    LatLng sydney = new LatLng(locationLatLong.getLatitude(), locationLatLong.getLongitude());
    MarkerOptions m1 = new MarkerOptions().draggable(true);
    mMap.addMarker(m1.position(sydney).title(ownerName));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
        @Override
        public void onMarkerDragStart(Marker marker) {

        }

        @Override
        public void onMarkerDrag(Marker marker) {

        }

        @Override
        public void onMarkerDragEnd(Marker marker) {
            markerLatLong = marker.getPosition();
        }
    });
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    try {
        locationLatLong = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
    } catch (SecurityException e) {
        e.printStackTrace();
    }
    mapFragment.getMapAsync(this);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();

}

@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

}

这是我的布局XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Edit Customer Info"
    android:textColor="@android:color/black" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Owner Name"
    android:textColor="@android:color/black"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editTextOwnerName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Enter Contact Person Name" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Owner Number"
    android:textColor="@android:color/black"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editTextOwnerNumber"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Enter Customer Number" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Latitude and Longitude"
    android:textColor="@android:color/black"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textViewLatLong"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="xxxxx"
    android:textColor="@android:color/black"
    android:textStyle="bold" />

<fragment
    android:id="@+id/mapDialog"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="200dp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/login_button"
        android:text="Save"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/buttonCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:background="@color/login_button"
        android:text="Cancel"
        android:textColor="@android:color/white" />
</LinearLayout>
</LinearLayout>

当我第一次打开对话框时,它会完美运行。

但是当我第二次打开它时,我得到了这个异常

Caused by: java.lang.IllegalArgumentException: Binary XML file line #55: Duplicate id 0x7f0d00a8, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
                                                                      at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293)
                                                                      at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                      at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
                                                                      at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                                      at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:294) 
                                                                      at android.app.Dialog.setContentView(Dialog.java:485) 
                                                                      at com.gfat.ui.DialogEditCustomer.<init>(DialogEditCustomer.java:62) 

如何摆脱这种情况?

1 个答案:

答案 0 :(得分:1)

不确定这是否是正确的过程但是有效

@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    FragmentManager fm = fragmentManager;
    Fragment fragment = (fm.findFragmentById(R.id.mapDialog));

    if (fragment.isResumed()) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.remove(fragment);
        ft.commit();
    }
    super.onStop();
}