私は Android 開発の初心者であり、問題が発生しました。解決していただければ幸いです。簡単に言うと...Google Placesのオートコンプリートをフラグメントで使用しようとしています...アプリは最初から停止します。私の推測では、問題はメソッドを使用するときと、その後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 からの Autocomplete リクエストを処理するアダプタ)
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