迁移到New Places SDK客户端 https://developers.google.com/places/android-sdk/client-migration
我正在尝试使用嵌入自动完成片段进行迁移,并且显然它可以正常工作,直到收到所选位置的返回时为止,因为下面的代码允许打开该片段,以便输入位置并在列表中选择它,但是此后,显示下面报告的错误,并且不执行 onPlaceSelected() 或 onError () 例程。
以下代码布局:
<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" />
以下代码功能:
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);
}
});
}
以下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)
我在logcat中注意到此函数正在返回:
W / MainActivity: onActivityResult (358101, -1, Intent {(has extras)})
在那之后,我创建了一个新的干净项目,只是为了测试该资源,并使用相同的代码和返回的函数:
W / MapsActivity: Place: Curitiba, ChIJ3bPNUVPj3JQRCejLuqVrL20 (rigth return in onPlaceSelected)
我认为新的 AutocompleteSupportFragment 中存在错误,因为不是在 onPlaceSelected 中返回呼叫,它将在 onActivityResult 中返回。
在两种情况下,相同的错误(W / System.err :)仍然存在,但始终在上述返回之后。
是否知道导致此问题的原因?
答案 0 :(得分:0)
我也通过致电
解决了这个问题super.onActivityResult(requestCode, resultCode, data);
该错误继续出现在LOG中,但现在至少响应是在 onPlaceSelected 而不是 onActivityResult
上返回的