java.lang.IllegalStateException:必须初始化位置 [英] java.lang.IllegalStateException: Places must be initialized

查看:570
本文介绍了java.lang.IllegalStateException:必须初始化位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是旧版SDK,效果很好,但是将要折旧,因此我改用新版SDK.我得到了一些设备的崩溃报告.

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.

崩溃报告:

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'

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

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.

                }
            });

布局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" />

推荐答案

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

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

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

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

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

第二步:

 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示例

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

这篇关于java.lang.IllegalStateException:必须初始化位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆