首页 > 解决方案 > 当第二次加载 GooglePlaceAutocomplete Fragment 时它崩溃了

问题描述

我正在努力PlaceAutocompleteFragment。我Fragment只是在第一次加载这个,当第二次加载时显示一些错误并且它崩溃了。

错误是:java.lang.IllegalArgumentException:二进制 XML 文件第 11 行:重复 id 0x7f090251、标签 null 或父 id 0x7f090193 与 com.google.android.gms.location.places.ui.PlaceAutocompleteFragment 的另一个片段

public class MakeReservationFragment extends BaseFragment implements 
 GoogleApiClient.OnConnectionFailedListener , PlaceSelectionListener {

  View mMakeReservationView;
  ProgressBar bar;
  FrameLayout layout;
  private static final LatLngBounds BOUNDS_GREATER_SYDNEY = new 
 LatLngBounds(
        new LatLng(-34.041458, 150.790100), new LatLng(-33.682247, 151.383362));

@Override
    public View setContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mMakeReservationView = inflater.inflate(R.layout.frag_make_reservation, container, false);
        initView();
        return mMakeReservationView;

    }

    private void initView() {

       setHeaderBarTitle("RESERVATION");

       PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
               getActivity().getFragmentManager().findFragmentById(R.id.place_fragment1);
       autocompleteFragment.setOnPlaceSelectedListener(MakeReservationFragment.this);
       autocompleteFragment.setHint("Search a Location");
       autocompleteFragment.setBoundsBias(BOUNDS_GREATER_SYDNEY);

        layout=(FrameLayout)mMakeReservationView.findViewById(R.id.framereserve1);
    }


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

    }

    @Override
    public void onResume() {
        super.onResume();
    getView().setFocusableInTouchMode(true);
    getView().requestFocus();

    setHeaderBarleftIcon(R.drawable.ic_home);
    getView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
           }
            return false;
        }
    });
    }
    @Override
    public void onPlaceSelected(Place place) {

    }

    @Override
    public void onError(Status status) {

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode ==RESULT_OK) {
                Place place = PlaceAutocomplete.getPlace(getActivity(), data);
                onPlaceSelected(place,0);
            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(getActivity(), data);
                this.onError(status,0);
            }
        }
        else
        {
            if (resultCode == RESULT_OK) {

                Place place = PlaceAutocomplete.getPlace(getActivity(), data);
                onPlaceSelected(place,1);
            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(getActivity(), data);
                onError(status,1);
            }
        }
    }

    private void onError(Status status, int i) {
        Toast.makeText(getActivity(), "Place selection failed: " + status.getStatusMessage(),
                Toast.LENGTH_SHORT).show();
    }

    private void onPlaceSelected(Place place, int i) {

            try{
                System.out.println("00000000 Place Darta IS  0  error" + place.getId()+","+place.getAddress()+","+place.getLatLng()+","+place.getLocale()+","+place.getName()+
                        ","+place.getAttributions()+","+place.getViewport()+","+place.getPhoneNumber()+","+place.getPlaceTypes());

            }
            catch ( Exception e)
            {

            }

     }

}

XML代码是:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/framereserve1"
android:background="@color/backgoound_dull"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
    android:id="@+id/place_fragment1"
    android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
    android:layout_width="match_parent"
    android:layout_height="50dp"/>

标签: androidandroid-fragmentsandroid-fragmentactivity

解决方案


推荐阅读