i wanna test a simple brief of using GPS on android. so i write a simple code like this :
public class main extends Activity{
LocationManager mLocationManager;
TextView tv;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.myLocationText);
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String locationprovider = mLocationManager.getBestProvider(criteria, true);
Location mLocation = mLocationManager.getLastKnownLocation(locationprovider);
tv.setText("Last location lat:" + mLocation.getLatitude() + "long:" + mLocation.getLongitude());
}
}
also, on AndroidManifest i added uses-permission like this :
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
but when i try to run at android emulator i get Error “Stopped Unexpectedly” and this apps closed. i added some try with DDMS too. but i dont have any idea about this error.
I’ve also had similar problems in real devices. The frequency at which the listener was receiving new locations was too high to update my TextViev. I solved it with a bit of synchronization, and skipping View refresh if it was being refreshed at that moment.
In your case, as you are not using a listener, I’d check for
NullPointerExceptions.