개발모음집

구글 검색 api 에러 본문

Android

구글 검색 api 에러

void 2017. 6. 30. 17:48

1. 참고 구글 개발자센터 이걸 보면서 장소자동완성텍스트를 만드려하는데


<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>

에서 Unresolved class 'PlaceAutocompleteFragment' 에러가 뜬다.


참고 스택오버플로우를 보니 '


compile 'com.google.android.gms:play-services-places:10.2.6'

를 해준다고 나온다. 에러해결!


2.  Inconvertible types; cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.location.places.ui.PlaceAutocompleteFragment

PlaceAutocompleteFragment autocompleteFragment =

(PlaceAutocompleteFragment) getFragmentManager()

.findFragmentById(R.id.place_autocomplete_fragment);

참고 스택오버플로우를 보니 getActivity()를 붙여주라하니 에러해결

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)

getActivity().getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);



전체 소스


 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment)
getActivity().getSupportFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

// AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
// .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
// .build();
// autocompleteFragment.setFilter(typeFilter);
// autocompleteFragment.setBoundsBias(new LatLngBounds(
// new LatLng(-33.880490, 151.184363),
// new LatLng(-33.858754, 151.229596)));

if(autocompleteFragment==null){
autocompleteFragment = (SupportPlaceAutocompleteFragment) SupportPlaceAutocompleteFragment.instantiate(getContext(), "com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment");
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Log.i("", "Place: " + place.getName());//get place details here
}

@Override
public void onError(Status status) {

}
});

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i("", "Place: " + place.getName());
}

@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i("", "An error occurred: " + status);
}
});
}
return inflater.inflate(R.layout.fragment_search, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}

xml


<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment"
/>