我是Android开发的初学者,我遇到了一个问题,希望你能帮我解决。我会保持简单......我正在尝试在片段中使用Google地方信息自动填充...应用程序从一开始就停止。我的猜测是问题是当我使用rebuildGoogleApiClient()
方法时以及之后设置PlaceAutocompleteAdapter
时。这是我的代码:
(ActivityMap.java
- 设置GoogleMap并创建片段)
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>
(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>
如果您需要一些额外的信息或额外的代码,请告诉我,并提前感谢您。
以下是日志:
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
答案 0 :(得分:3)
我认为问题是你在enableAutoManage()
中使用Fragment.onCreateView()
。
相反,我建议如下:
GoogleApiClient
Fragment.onCreate()
mGoogleApiClient.connect()
Fragment.onStart()
mGoogleApiClient.disconnect()
Fragment.onStop()
有关详细信息,请参阅https://developers.google.com/places/android/start#connect-client。
我希望这有帮助!