Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Kotlin-无法获取实际设备中的位置地址_Java_Android_Google Maps_Kotlin_Android Service - Fatal编程技术网

Java Kotlin-无法获取实际设备中的位置地址

Java Kotlin-无法获取实际设备中的位置地址,java,android,google-maps,kotlin,android-service,Java,Android,Google Maps,Kotlin,Android Service,我的小应用程序有麻烦了 我想在android studio中使用反向地理编码获得地址位置 但是我的代码只是在模拟器中工作。当我把我真正的dvice和应用程序连接起来,想要得到地址时,它给了我一个错误,应用程序崩溃了。错误:grpc失败 我将代码放在try-and-catch方法中,但错误仍然存在 这是我的小应用程序: 梅尼费斯特: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns

我的小应用程序有麻烦了

我想在android studio中使用反向地理编码获得地址位置

但是我的代码只是在模拟器中工作。当我把我真正的dvice和应用程序连接起来,想要得到地址时,它给了我一个错误,应用程序崩溃了。错误:grpc失败

我将代码放在try-and-catch方法中,但错误仍然存在

这是我的小应用程序:

梅尼费斯特:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.my_test_app">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyCutiOvAqZiLvka-kW9B12K.........." />

    </application>

</manifest>

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    >


    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="text1"
        android:textSize="20sp"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text2"
        android:layout_marginTop="10dp"
        android:textSize="20sp"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/textview1"
        />


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textview2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="button"
        android:textSize="25sp"
        />



</RelativeLayou

如果(位置!=null){
val lat=位置。纬度
val long=location.longitude
val geocoder=geocoder(这是Locale.getDefault())
试一试{
val地址:List=geocoder.getFromLocation(lat,long,1)
textview1.text=地址[0]。maxAddressLineIndex.toString()
textview2.text=地址[0]。getAddressLine(0)。toString()
}捕获(e:例外){
Toast.makeText(this,e.message.toString(),Toast.LENGTH\u LONG.show())
}
}否则{
Toast.makeText(这是“Toast.LENGTH”.show()
}
}
}
}
那么,我的代码有什么问题?Grpc错误的含义是什么

如果我把地理代码放在后线程中,问题是否会解决

有人能帮忙吗

我使用的调试api密钥不是release


感谢您的帮助。

需要在应用程序主线程之外调用Geocoder

Geocoder是同步的,可能需要很长时间才能完成工作,因此您不应该从应用程序的主用户界面(UI)线程调用它

尝试使用
AsyncTask


还要检查您是否有良好的internet和未使用代理。

我搜索了AsyncTask,不推荐使用。你知道我在后台做代码要做什么吗?
package com.example.my_test_app

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.Address
import android.location.Geocoder
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.loader.content.AsyncTaskLoader
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import com.google.android.gms.tasks.OnSuccessListener
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {

    lateinit var fusedLocationProviderClient: FusedLocationProviderClient
    private val requestcode = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val button = findViewById<Button>(R.id.button)



        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)

        button.setOnClickListener {

            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.ACCESS_FINE_LOCATION
                ) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.ACCESS_COARSE_LOCATION
                ) == PackageManager.PERMISSION_GRANTED
            ) {

                getlocation()


            } else {

                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION
                    ),
                    requestcode
                )

                return@setOnClickListener

            }


        }

    }

    private fun getlocation() {

        fusedLocationProviderClient.lastLocation.addOnSuccessListener(this) { location ->


            if (location != null) {

                val lat = location.latitude
                val long = location.longitude

                val geocoder = Geocoder(this, Locale.getDefault())

                try {

                    val address: List<Address> = geocoder.getFromLocation(lat, long, 1)

                    textview1.text = address[0].maxAddressLineIndex.toString()
                    textview2.text = address[0].getAddressLine(0).toString()


                } catch (e: Exception) {

                    Toast.makeText(this, e.message.toString(), Toast.LENGTH_LONG).show()
                }


            } else {


                Toast.makeText(this, "موقعیت پیدا نشد ", Toast.LENGTH_LONG).show()

            }

        }


    }


}