Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

android - java.lang.IllegalStateException: Places must be initialized

I was using the old Place SDK which was working fine But it is going to be depreciated and I move to new place SDK. I got keep the crashing report from a few devices.

Crashes Report:

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 version:

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

Even i initialize Place sample code:

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.

                }
            });

Layout 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" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

 dependencies {
      implementation 'com.google.android.libraries.places:places:2.0.0'
    }

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...