I am a begginer in Android programming and I encountered a problem with displaying the current location of my phone using the NETWORK PROVIDER, with which I need some help.
Here is the code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.txtLat);
txtLon = (TextView) findViewById(R.id.txtLon);
Log.d("ADebugTag", "WTF");
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
txtLat.setText(String.valueOf(location.getLatitude()));
txtLon.setText(String.valueOf(location.getLongitude()));
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Log.d("ADebugTag", "WTF END");
}
I try to set the latitude and longitude of the location into the TextViews, but my app crashes when i try tu run it. Also, I tried to print a full stack trace, but I couldn't find a method to make it work..
UPDATE: Thanks for all the replies :D - After posting this I realized I should have also posted the layout of the main activity, just in case.
<TextView
android:id = "@+id/txtLat"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="LATITUDINE" />
<TextView
android:id = "@+id/txtLon"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/txtLat"
android:text="LONGITUDINE" />
Also, I have tried to print something in logcat when the onLocationChanged method gets called, but the message is not displayed when I run the app, meaning that the method is not called. I am running this app on an older device, using Android 2.3.6, maybe that is useful information as well.