我正在使用FusedLocation提供商,即使我的手机中的Internet Services已关闭,我也不希望我的应用程序崩溃。我尝试使用尝试/捕获,没有用

发布于 2025-02-10 03:18:31 字数 252 浏览 1 评论 0原文

注意:互联网服务故意在我的手机中关闭。

在下行中出现错误:

addresses = geocoder.getFromLocation(latitude, longitude, 1)

即使Internet服务已关闭,我也不希望我的应用程序崩溃,并希望将问题捕获到Catch Block中。 我已经实现了try/catch块,但是我的应用程序仍然通过给出以下例外而

崩溃 提前致谢。

NOTE: Internet services are OFF intentionally in my phone.

Getting error at the below line:

addresses = geocoder.getFromLocation(latitude, longitude, 1)

I don't want my application to crash even if the internet service is off and want the issue to be caught in catch block.
I have implemented the try/catch block, but still my app crashed by giving the below exception:

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

is there any solution for my problem.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滿滿的愛 2025-02-17 03:18:31

您应该在单独的线程

private fun getAddress(latitude: Double, longitude: Double) {
    viewModelScope.launch {
        try {
            var addresses: List<Address>
            val deferred = this.async(Dispatchers.IO) {
                addresses = geocoder!!.getFromLocation(
                    latitude,
                    longitude,
                    1
                ) // Here 1 represent max location result to returned, by documents it recommended 1 to 5

                return@async addresses
            }
            withContext(Dispatchers.Main) {
                val data = deferred.await()
                showAddress(data)
            }

        } catch (e: Exception) {
            Log.e("location", e.message!!)
        }
    }
}

检索地址上调用地理编码器

 private fun showAddress(addresses: List<Address>) {
    val address = addresses[0]
        .getAddressLine(0) // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()

    val city = addresses[0].locality
    val state = addresses[0].adminArea
    val country = addresses[0].countryName
    val postalCode = addresses[0].postalCode
    val knownName = addresses[0].featureName // Only if available else return NULL

}

You should call Geocoder on a separate thread

private fun getAddress(latitude: Double, longitude: Double) {
    viewModelScope.launch {
        try {
            var addresses: List<Address>
            val deferred = this.async(Dispatchers.IO) {
                addresses = geocoder!!.getFromLocation(
                    latitude,
                    longitude,
                    1
                ) // Here 1 represent max location result to returned, by documents it recommended 1 to 5

                return@async addresses
            }
            withContext(Dispatchers.Main) {
                val data = deferred.await()
                showAddress(data)
            }

        } catch (e: Exception) {
            Log.e("location", e.message!!)
        }
    }
}

Retrieve address:

 private fun showAddress(addresses: List<Address>) {
    val address = addresses[0]
        .getAddressLine(0) // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()

    val city = addresses[0].locality
    val state = addresses[0].adminArea
    val country = addresses[0].countryName
    val postalCode = addresses[0].postalCode
    val knownName = addresses[0].featureName // Only if available else return NULL

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文