gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:02:17 24 4
gpt4 key购买 nike

我试图在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com