1

I am trying to show the current Address of a user in an EditText. Therefore I use a Location manager and a LocationListener. The App compiles just like it should...however the EditText is showing nothing excepted the hint. For me, it seems like the App is not even going into the try{} statement?! Of course, I gave the app permission to use my Location...I have the problem on a real device... on the Emulator the App runs fine. Also when I change the Location on the Emulator nothing happens. Logcat is only showing the Location (Longitude and Latitude etc.) defined in the OnlocationChanged Method. Why is my Address not showing?

Manifest

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

A Part of my Activity in which I want to show the Adress:

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
Jim Simson
  • 2,774
  • 3
  • 22
  • 30
AnonRocketman
  • 178
  • 2
  • 16

1 Answers1

0

Probably its the Geocoder. Geocoder need internet connection and sometimes having this issue Service not available while calling geoCoder.getFromLocation(). If you are having this logcat error;

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

try rebooting your device. Also you cannot really rely on Geocoder on getting the address all the time.

L2_Paver
  • 596
  • 5
  • 11
  • 1
    I looked for the error above but my Logcat seems alright. Somehow it is now working on the Emulator just fine but on a real device, I have still the same problem. – AnonRocketman Sep 23 '19 at 12:58
  • Like I said geocoder need internet connection, I notice above that you didn't have permission for the Internet. Try adding internet permission in your manifest – L2_Paver Sep 23 '19 at 13:18
  • 1
    Added Internet permission to the Manifest. The real device which is connected to the Internet is still facing the same problem. – AnonRocketman Sep 23 '19 at 13:33