Google Places AutoComplete + Android Fragment

BlueHatCoder picture BlueHatCoder · May 7, 2015 · Viewed 9k times · Source

I am a beginner on Android development and I came into a problem which I hope you can help me to solve. I'll keep it simple...i am trying to use Google Places autocompletion in a fragment...the app stops right from the beginning. My guess is the problem is when I use the method rebuildGoogleApiClient() and when I set up the PlaceAutocompleteAdapter after that. Here is my code:

(ActivityMap.java - sets up the GoogleMap and creates the fragment)

    public class ActivityMap extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_map);

            (...)

        }

        //creates fragment
        private void selectItem(int position) {
            (...)
            MyFragment fragment = new MyFragment();

            FragmentManager fragmentManager = this.getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.content_frame, fragment);
            fragmentTransaction.commit();
        }

    }   

(MyFragment.java - contains the AutoCompleteTextView)

    public class MyFragment extends Fragment implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {

        View rootView;
        AutoCompleteTextView from;
        GoogleApiClient mGoogleApiClient;
        PlaceAutocompleteAdapter mAdapter;

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

            rootView = inflater.inflate(R.layout.myFragment, container, false);

            if (mGoogleApiClient == null) {
                rebuildGoogleApiClient();
            }

            from = (AutoCompleteTextView) rootView.findViewById(R.id.autoCompleteFrom);

            (...)

            mAdapter = new PlaceAutocompleteAdapter(this.getActivity(), android.R.layout.simple_list_item_1, null, null);
            from.setAdapter(mAdapter);

            return rootView;
        }


        private void rebuildGoogleApiClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(this.getActivity()).enableAutoManage(this.getActivity(), 0 /* clientId */, this).addConnectionCallbacks(this).addApi(Places.GEO_DATA_API).build();
        }

    }

(PlaceAutocompleteAdapter - Adapter that handles Autocomplete requests from the Places Geo Data API )

    public class PlaceAutocompleteAdapter extends ArrayAdapter<PlaceAutocompleteAdapter.PlaceAutocomplete> implements Filterable {

        LatLngBounds mBounds;
        AutocompleteFilter mPlaceFilter;

        //constructor
        public PlaceAutocompleteAdapter(Context context, int resource,LatLngBounds bounds, AutocompleteFilter filter) {
            super(context, resource);
            mBounds = bounds;
            mPlaceFilter = filter;
        }


    }

(activity_map.xml)

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <fragment
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>
    </LinearLayout>

(my_fragment.xml)

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/from"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="From" />

        <AutoCompleteTextView
            android:id="@+id/autoCompleteFrom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:completionThreshold="2" />
    </RelativeLayout>

Let me know if you need some extra info or extra code and thank you in advance.

Here are the logs:

    05-07 16:52:55.840: E/AndroidRuntime(2664):     FATAL EXCEPTION: main

    05-07 16:52:55.840: E/AndroidRuntime(2664):     Process: com.example.blueHatCoder.myapplication1, PID: 2664

    05-07 16:52:55.840: E/AndroidRuntime(2664):     java.lang.IllegalStateException: Recursive entry to executePendingTransactions

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1473)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at com.google.android.gms.common.api.zzh.zza(Unknown Source)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at com.google.android.gms.common.api.GoogleApiClient$Builder.zzhZ(Unknown Source)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at com.google.android.gms.common.api.GoogleApiClient$Builder.build(Unknown Source)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at com.example.blueHatCoder.myapplication1.MyFragment.rebuildGoogleApiClient(MyFragment.java:155)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at com.example.blueHatCoder.myapplication1.MyFragment.onCreateView(MyFragment.java:51)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.os.Handler.handleCallback(Handler.java:739)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.os.Handler.dispatchMessage(Handler.java:95)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.os.Looper.loop(Looper.java:135)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at android.app.ActivityThread.main(ActivityThread.java:5221)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at java.lang.reflect.Method.invoke(Native Method)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at java.lang.reflect.Method.invoke(Method.java:372)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

    05-07 16:52:55.840: E/AndroidRuntime(2664):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

    05-07 16:52:55.865: W/ActivityManager(1222):    Force finishing activity com.example.blueHatCoder.myapplication1/.ActivityMap

Answer

Francois Wouts picture Francois Wouts · May 8, 2015

I believe the problem is that you're using enableAutoManage() in Fragment.onCreateView().

Instead, I would recommend the following:

  • instantiate the GoogleApiClient in Fragment.onCreate()
  • call mGoogleApiClient.connect() in in Fragment.onStart()
  • call mGoogleApiClient.disconnect() in Fragment.onStop()

See https://developers.google.com/places/android/start#connect-client for details.

I hope this helps!