java.lang.IllegalStateException: Places must be initialized

微笑、不失礼 提交于 2019-12-01 17:55:13

I'm also facing the same issue but I resolved with this code.

First Step: Initialize the Places SDK In OnCreate Method or you can initialize it on your Application Class

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

Second Step:

 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)

And in Activity Result

   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());
            }
        }
}

This is the Kotlin Example but you can convert in JAVA Also, you can refer to this URL for Examples. Google Places SDK Example

You must initiase the google places library, below is the code:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!