android - Google Places 自动完成 + Android fragment

标签 android android-fragments autocomplete google-api-client android-googleapiclient

我是 Android 开发的初学者,我遇到了一个问题,希望你能帮助我解决。我会保持简单......我正在尝试在 fragment 中使用 Google Places 自动完成......应用程序从一开始就停止。我的猜测是问题出在我使用 rebuildGoogleApiClient() 方法以及之后设置 PlaceAutocompleteAdapter 时。这是我的代码:

(ActivityMap.java - 设置 GoogleMap 并创建 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 - 包含 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 - 处理来自 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>

(我的 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>

如果您需要一些额外的信息或额外的代码,请告诉我,在此先感谢您。

这是日志:

    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

最佳答案

我认为问题在于您在 Fragment.onCreateView() 中使用了 enableAutoManage()

相反,我会推荐以下内容:

  • Fragment.onCreate() 中实例化 GoogleApiClient
  • Fragment.onStart() 中调用 mGoogleApiClient.connect()
  • Fragment.onStop() 中调用 mGoogleApiClient.disconnect()

参见 https://developers.google.com/places/android/start#connect-client了解详情。

希望对您有所帮助!

关于android - Google Places 自动完成 + Android fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30089123/

相关文章:

java - 应用程序正在等待调试器

android - 如何从我自己的 Gradle 插件中确定正在使用的 Android Gradle 插件版本?

java - 如何在Android中获取当前位置

javascript - 文本区域建议提供者

javascript - 如何更快地索引 Javascript 中的对象数组?

android - 如果Android版本低于3.0,如何隐藏操作栏(Theme.Holo)

android - 将 fragment 实例保留在 FragmentPagerAdapter 中

java - 无法更改嵌套 fragment 中的 ActionBar 标题

Android ViewPager 加载不正确的 fragment

ios - 如何过滤 Google AutoComplete API 的结果以仅显示完整地址