java.lang.IllegalStateException:地方必须初始化

时间:2019-05-07 10:41:42

标签: android google-places-api googleplacesautocomplete

我使用的旧版SDK可以正常运行,但是将要折旧,因此我改用新版的SDK。我得到了一些设备的崩溃报告。

崩溃报告:

Fatal Exception: java.lang.RuntimeException
Unable to start activity ComponentInfo{com.islamuna.ramadan/com.google.android.libraries.places.widget.AutocompleteActivity}: java.lang.IllegalStateException: Places must be initialized.

SDK版本:

    implementation 'com.google.android.libraries.places:places:1.1.0'

即使我初始化放置示例代码:

Places.initialize(getApplicationContext(), "mykey", Locale.US);

autocompleteFragment = (AutocompleteSupportFragment)
                    getSupportFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
            autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME,Place.Field.LAT_LNG));
            autocompleteFragment.setText(Global.getStoredStringValue(getApplicationContext(), getString(R.string.KEY_CITY)));

            autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
                @Override
                public void onPlaceSelected(Place place) {
                    try {

                        }
                    } catch (Exception e) {
                    }

                }

                @Override
                public void onError(Status status) {
                    // TODO: Handle the error.

                }
            });

布局XML

<fragment
                    android:id="@+id/place_autocomplete_fragment"
                    android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/border_filled" />

2 个答案:

答案 0 :(得分:3)

我也遇到了同样的问题,但是我用此代码解决了。

第一步:在OnCreate方法中初始化Places SDK,也可以在应用程序类上对其进行初始化

if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(), getString(R.string.api_key), Locale.US);
    }

第二步:

 var fields=Arrays.asList(Place.Field.ID,Place.Field.NAME,Place.Field.LAT_LNG)
    var intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this)
     startActivityForResult(intent, PLACE_PICKER_REQUEST)

活动结果中

   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == Activity.RESULT_OK) {
                val place =Autocomplete.getPlaceFromIntent(data);

                lat = place.latLng?.latitude
                lng = place.latLng?.longitude
            }
            else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                // TODO: Handle the error.
                var status = Autocomplete.getStatusFromIntent(data)
                Log.i("address", status.getStatusMessage());
            }
        }
}

这是Kotlin示例,但您可以在JAVA中进行转换 另外,您可以参考此URL以获得示例。 Google Places SDK Example

答案 1 :(得分:-1)

您必须启动Google地方信息库,下面是代码:

Places.initialize(getApplicationContext(),getString(R.string.api_key));