Migrating to the New Places SDK Client https://developers.google.com/places/android-sdk/client-migration
I am trying to do the migration using Embed an AutocompleteFragment and apparently it works correctly until at the moment of receiving the return of the selected location, because the code below allows to open the fragment, to enter the location and to select it in a list, but after that, presents the error reported below and does not execute the onPlaceSelected() or onError() routine.
The following code layout:
<fragment
android:id="@+id/autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment" />
The following code function:
public void mostraPesquisa(boolean set) {
if (MainActivity.this.isDestroyed()) return;
if (!Places.isInitialized()) { Places.initialize(MainActivity.this, getResources().getString(R.string.google_maps_key)); }
// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.getView().setVisibility(set ? VISIBLE : GONE);
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(@NonNull Place place) {
Log.w(TAG, "Place: " + place.getName() + ", " + place.getId());
}
@Override
public void onError(@NonNull Status status) {
Log.e(TAG, "AutocompleteSupportFragment - Ocorreu um erro: " + status);
}
});
}
Following logcat:
W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
W/libEGL: EGLNativeWindowType 0x6f97da4010 disconnect failed
E/SchedPolicy: set_timerslack_ns write failed: Operation not permitted
W/MainActivity: onActivityResult(95957,-1,Intent { (has extras) })
W/libEGL: EGLNativeWindowType 0x6f753f4010 disconnect failed
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
W/System.err: at android.view.ViewRootImpl.getHostVisibility(ViewRootImpl.java:1786)
W/System.err: at android.view.ViewRootImpl.handleAppVisibility(ViewRootImpl.java:1422)
W/System.err: at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4818)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err: at android.os.Looper.loop(Looper.java:214)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7073)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
I noticed in logcat that this function is returning:
W / MainActivity: onActivityResult (358101, -1, Intent {(has extras)})
After that, i created a new clean project just to test this resource and using the same code and the function returned:
W / MapsActivity: Place: Curitiba, ChIJ3bPNUVPj3JQRCejLuqVrL20 (rigth return in onPlaceSelected)
I think there is a bug in the new AutocompleteSupportFragment, because instead of returning the call in onPlaceSelected, it is returning in onActivityResult.
The same error (W/System.err:) persist in both cases, but always after the above returns.
Any idea of the cause of this issue ?
I fixed this as well by calling
super.onActivityResult(requestCode, resultCode, data);
The error continues to appear in the LOG, but now at least the response is returning on the onPlaceSelected instead of on the onActivityResult