I develop simple application that detects the location of android device and display its values in text Views
The application crashes once it starts throwing RuntimeException.
Following is GPS.java
public class GPS extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria,false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.d("PROVIDER", "Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}
Log is as following :
10-09 16:49:35.733: E/AndroidRuntime(1616): FATAL EXCEPTION: main 10-09 16:49:35.733: E/AndroidRuntime(1616): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gps/com.example.gps.GPS}: java.lang.NullPointerException 10-09 16:49:35.733: E/AndroidRuntime(1616): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 10-09 16:49:35.733: E/AndroidRuntime(1616): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 10-09 16:49:35.733: E/AndroidRuntime(1616): at android.app.ActivityThread.access$600(ActivityThread.java:123) 10-09 16:49:35.733: E/AndroidRuntime(1616): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 10-09 16:49:35.733: E/AndroidRuntime(1616): at android.os.Handler.dispatchMessage(Handler.java:99) 10-09 16:49:35.733: E/AndroidRuntime(1616): at android.os.Looper.loop(Looper.java:137) 10-09 16:49:35.733: E/AndroidRuntime(1616): at android.app.ActivityThread.main(ActivityThread.java:4424) 10-09 16:49:35.733: E/AndroidRuntime(1616): at java.lang.reflect.Method.invokeNative(Native Method) 10-09 16:49:35.733: E/AndroidRuntime(1616): at java.lang.reflect.Method.invoke(Method.java:511) 10-09 16:49:35.733: E/AndroidRuntime(1616): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 10-09 16:49:35.733: E/AndroidRuntime(1616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 10-09 16:49:35.733: E/AndroidRuntime(1616): at dalvik.system.NativeStart.main(Native Method) 10-09 16:49:35.733: E/AndroidRuntime(1616): Caused by: java.lang.NullPointerException 10-09 16:49:35.733: E/AndroidRuntime(1616): at com.example.gps.GPS.onCreate(GPS.java:44) 10-09 16:49:35.733: E/AndroidRuntime(1616): at android.app.Activity.performCreate(Activity.java:4465) 10-09 16:49:35.733: E/AndroidRuntime(1616): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 10-09 16:49:35.733: E/AndroidRuntime(1616): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 10-09 16:49:35.733: E/AndroidRuntime(1616): ... 11 more