java - 在 EditText 中显示地址在真实设备上不起作用

标签 java android location locationmanager

我试图在 EditText 中显示用户的当前地址。因此,我使用位置管理器和位置监听器。该应用程序按应有的方式编译...但是 EditText 除了提示外什么也没显示。 对我来说,App 似乎甚至没有进入 try{} 语句?!当然,我允许该应用程序使用我的位置...我在真实设备上遇到问题...在模拟器上应用程序运行良好。此外,当我更改模拟器上的位置时,什么也没有发生。 Logcat 仅显示 OnlocationChanged 方法中定义的位置(经度和纬度等)。 为什么我的地址没有显示?

list

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>

我要在其中显示地址的 Activity 的一部分:

public class NavActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private AppBarConfiguration mAppBarConfiguration;
private FirebaseAuth firebaseAuth;
public EditText Position;

LocationManager locationManager;
LocationListener locationListener;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

     //Some more Lines of nonrelated Code
     //... 
       
    firebaseAuth=FirebaseAuth.getInstance();

    //Anfang Location

    locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    locationListener=new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.i("Location:",location.toString());
            Geocoder geocoder=new Geocoder(getApplicationContext(), Locale.getDefault());

            try{
                List<Address> listAdresses=geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
                if(listAdresses != null && listAdresses.size()>0){
                    String adress=" ";

                    if(listAdresses.get(0).getPostalCode() != null){
                        adress += listAdresses.get(0).getPostalCode()+" ";
                    }
                    if(listAdresses.get(0).getSubThoroughfare() != null) {
                        adress += listAdresses.get(0).getSubThoroughfare() + " ";
                    }
                    if(listAdresses.get(0).getCountryName() != null) {
                        adress += listAdresses.get(0).getCountryName() + " ";
                    }
                    if(listAdresses.get(0).getSubLocality() != null) {
                        adress += listAdresses.get(0).getSubLocality() + " ";
                    }
                    if(listAdresses.get(0).getThoroughfare() != null) {
                        adress += listAdresses.get(0).getThoroughfare() + " ";
                    }

                    Toast.makeText(NavActivity.this,adress,Toast.LENGTH_SHORT).show();
                    Log.i("Standort",adress);

                    Position = (EditText)findViewById(R.id.myEditDAdresse);
                    Position.setText("Standort:"+ adress);
                }

            }catch(Exception e){
                Position = (EditText)findViewById(R.id.myEditDAdresse);
                Position.setText("Standortsuche fehlgeschlagen");


            }
        }
        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }
        @Override
        public void onProviderEnabled(String s) {

        }
        @Override
        public void onProviderDisabled(String s) {

        }
    };

    if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
    }else{
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
    }

}

//Ende Location

最佳答案

可能是地理编码器。 Geocoder 需要互联网连接,有时会出现此问题 Service not available while calling geoCoder.getFromLocation() . 如果您遇到此 logcat 错误;

E/Exception in getLocationDetails -(10966): Service not Available

尝试重新启动您的设备。 此外,您不能真正依赖 Geocoder 一直获取地址。

关于java - 在 EditText 中显示地址在真实设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58053794/

相关文章:

java - 范围求和多线程

java - Groovy xml 解析

android - Android.Sqlite 和 Eclipse 中的初级帮助

Swift 4 MapKit 检查标记是否靠近我的位置

java.lang.NoSuchMethodError : No virtual method toString(Z)Ljava/lang/String; in class Lokhttp3/Cookie; or its super classe

Javacc 包问题

java - 如何关闭另一个类的 Intent Activity

android - 使用单例对象来保存状态或在 Activity 之间共享数据是否可以接受?

ios - 调用函数 swift 时使用另一个类获取位置不起作用

android - 如何以编程方式检查 Android "Location Consent"?