新 Google 地点自动完成在点击时崩溃
New Google Places Autocomplete crashes on Click
我使用了 Google 个地方的新依赖项。单击自动完成视图时应用程序崩溃。报错如下,
java.lang.NullPointerException: Place Fields must be set.
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:889)
at com.google.android.libraries.places.internal.dt.onClick(Unknown Source)
at android.view.View.performClick(View.java:6207)
at android.widget.TextView.performClick(TextView.java:11094)
at android.view.View$PerformClick.run(View.java:23639)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
启动方法。我在 Fragment 中使用了它,所以我用 getFragmentManager() 来初始化 Fragment。
private void setUpLocationPicker(){
if (!Places.isInitialized()) {
Places.initialize(this.getActivity(), getString(R.string.google_maps_key));
}
// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
}
以及 XML 布局。
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/autocomplete_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pickup_address" />
</android.support.constraint.ConstraintLayout>
为什么在 Onclick 事件中自动完成视图 Returns null。如何解决?
我找到了这样做的方法,post 答案在这里。
// Initialize place API
if (!Places.isInitialized()) {
Places.initialize(mContext, mContext.getString(R.string.google_maps_key));
}
PlacesClient placesClient = Places.createClient(mContext);
// Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
// and once again when the user makes a selection (for example when calling fetchPlace()).
AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
// Specify the fields to return.
List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG);
另外我从 Place Migration Guide 那里得到了答案。希望这会有所帮助。
如果对 someone.In 我的案例有帮助,请稍后回答 android:noHistory="true"
像我一样解决问题 android:noHistory="false"
它又开始工作了
我使用了 Google 个地方的新依赖项。单击自动完成视图时应用程序崩溃。报错如下,
java.lang.NullPointerException: Place Fields must be set.
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:889)
at com.google.android.libraries.places.internal.dt.onClick(Unknown Source)
at android.view.View.performClick(View.java:6207)
at android.widget.TextView.performClick(TextView.java:11094)
at android.view.View$PerformClick.run(View.java:23639)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
启动方法。我在 Fragment 中使用了它,所以我用 getFragmentManager() 来初始化 Fragment。
private void setUpLocationPicker(){
if (!Places.isInitialized()) {
Places.initialize(this.getActivity(), getString(R.string.google_maps_key));
}
// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
}
以及 XML 布局。
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/autocomplete_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pickup_address" />
</android.support.constraint.ConstraintLayout>
为什么在 Onclick 事件中自动完成视图 Returns null。如何解决?
我找到了这样做的方法,post 答案在这里。
// Initialize place API
if (!Places.isInitialized()) {
Places.initialize(mContext, mContext.getString(R.string.google_maps_key));
}
PlacesClient placesClient = Places.createClient(mContext);
// Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
// and once again when the user makes a selection (for example when calling fetchPlace()).
AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
// Specify the fields to return.
List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG);
另外我从 Place Migration Guide 那里得到了答案。希望这会有所帮助。
如果对 someone.In 我的案例有帮助,请稍后回答 android:noHistory="true"
像我一样解决问题 android:noHistory="false"
它又开始工作了