I follow this post What is the simplest and most robust way to get the user's current location on Android?
I have many questions about the post, but unfortunately I don't have enough reputation to reply there, so I have to create a separate post. Sorry for any inconvenience.
Copy and past the code into my app and it works on my Samsung S2, but doesn't work in the emulator (2.3).
My questions:
When run in the emulator, the code never reach the onLocationChanged() methods, instead it will wait for 20 sec (timeout set by the timer1) to start run() of GetLastLocation. Why?
Now, the run() method of GetLastLocation class is always called, and I got the exception above.
(E/AndroidRuntime(436): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.).
I guess the error occurs in my code:
public LocationResult locationResult = new LocationResult()
{
@Override
public void gotLocation(final Location location)
{
Log.i(LOG_TAG, "gotLocation");
if (location == null)
{
Log.i(LOG_TAG, "Cannot get location");
}
else
{
Log.i(LOG_TAG, "Lat: " + location.getLatitude() + " Long:" + location.getLongitude());
tv = (TextView) findViewById(R.id.my_text); // <<< error here!!!
tv.setText(lat + "," + lon);
}
}
};
TextView tv is initialized in my main Activity, which calls MyLocation myLocation = new MyLocation();
and myLocation.getLocation(this, locationResult);
. The design is the same with that of the post. I wonder if the run() of GetLastLocation is called in another thread? Why is this? Has anyone got the same issue?
Thanks a lot!