• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Android application Crashes

Deewanagan

Lurker
Jan 18, 2010
4
0
hello everyone,

I am very new to android programming. i was following a guide in here, to create an application that shows the current location, i did as was told in the guide, but whenever i try to run it, it crashes, and asks for "force Close",:(
does somebody know what i am doing wrong? here is the code:
Code:
package pro.googleLocation;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class Finder extends Activity {
    /** Called when the activity is first created. */
    private LocationManager myLocationManager;
    private LocationListener myLocationListener;
    private TextView myLatitude, myLongitude;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myLatitude = (TextView)findViewById(R.id.Latitude);
        myLongitude = (TextView)findViewById(R.id.Longitude);

        myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        myLocationListener = new MyLocationListener();
        myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myLocationListener);
        myLatitude.setText(String.valueOf(
                  myLocationManager.getLastKnownLocation(
                    LocationManager.GPS_PROVIDER).getLatitude()));

                myLongitude.setText(String.valueOf(
                 myLocationManager.getLastKnownLocation(
                   LocationManager.GPS_PROVIDER).getLongitude()));
    }
    private class MyLocationListener implements LocationListener{

        public void onLocationChanged(Location argLocation) {
        // TODO Auto-generated method stub
        myLatitude.setText(String.valueOf(
          argLocation.getLatitude()));
        myLongitude.setText(String.valueOf(
          argLocation.getLongitude()));
        }

        public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        }

        public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        }

        public void onStatusChanged(String provider,
         int status, Bundle extras) {
        // TODO Auto-generated method stub
        }
        };
        
}

log cat shows something like this:
Code:
01-18 22:12:46.017: WARN/dalvikvm(1091): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
01-18 22:12:46.017: ERROR/AndroidRuntime(1091): Uncaught handler: thread main exiting due to uncaught exception
01-18 22:12:46.037: ERROR/AndroidRuntime(1091): java.lang.RuntimeException: Unable to start activity ComponentInfo{pro.googleLocation/pro.googleLocation.Finder}: java.lang.NullPointerException
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at android.os.Looper.loop(Looper.java:123)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at android.app.ActivityThread.main(ActivityThread.java:4203)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at java.lang.reflect.Method.invoke(Method.java:521)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at dalvik.system.NativeStart.main(Native Method)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091): Caused by: java.lang.NullPointerException
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at pro.googleLocation.Finder.onCreate(Finder.java:28)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
01-18 22:12:46.037: ERROR/AndroidRuntime(1091):     ... 11 more
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones