如何从非活动类调用位置管理器

时间:2015-03-06 03:55:42

标签: android nullpointerexception gps

请告诉我如何解决。

class t1  implements Runnable {
           private Thread t;
        private Context mContext;
               public void run() { GPSTracker gps = gps = new GPSTracker(mContext);}
     public void start ()
           { t = new Thread ();          t.start ();
                   }
        }
    public class GPSTracker extends Service implements LocationListener {
        private final Context mContext;
        boolean isGPSEnabled = false;
        boolean isNetworkEnabled = false;
        boolean canGetLocation = false;
        Location location; 
        double latitude; 
        double longitude;
        private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 
        private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; 
        protected LocationManager locationManager;
        public GPSTracker(Context context) {
            this.mContext = context;
            getLocation();
        }
        public Location getLocation() {
            try {
 
    // my code is not passing into the below line showing given error           
  locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
                isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            if (!isGPSEnabled && !isNetworkEnabled) {
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                if (isGPSEnabled) {
                if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {         
        }
        return location;
    }
    public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(GPSTracker.this);
        }       
    }
    public double getLatitude(){
        if(location != null){
            System.out.println("S66");
            latitude = location.getLatitude();
        }

        // return latitude
        return latitude;
    }
    public double getLongitude(){
        if(location != null){
            System.out.println("S67");
            longitude = location.getLongitude();
        }

        return longitude;
    }
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    @Override
    public void onLocationChanged(Location location) {
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

当我运行此代码时出现此错误:

03-06 03:22:20.017: E/AndroidRuntime(908): FATAL EXCEPTION: main
03-06 03:22:20.017: E/AndroidRuntime(908): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.g1/com.example.g1.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.os.Looper.loop(Looper.java:137)
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-06 03:22:20.017: E/AndroidRuntime(908):  at java.lang.reflect.Method.invokeNative(Native Method)
03-06 03:22:20.017: E/AndroidRuntime(908):  at java.lang.reflect.Method.invoke(Method.java:511)
03-06 03:22:20.017: E/AndroidRuntime(908):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-06 03:22:20.017: E/AndroidRuntime(908):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-06 03:22:20.017: E/AndroidRuntime(908):  at dalvik.system.NativeStart.main(Native Method)
03-06 03:22:20.017: E/AndroidRuntime(908): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.app.Activity.getSystemService(Activity.java:4463)
03-06 03:22:20.017: E/AndroidRuntime(908):  at com.example.g1.GPSTracker.<init>(GPSTracker.java:46)
03-06 03:22:20.017: E/AndroidRuntime(908):  at com.example.g1.t1.run(t1.java:23)
03-06 03:22:20.017: E/AndroidRuntime(908):  at com.example.g1.MainActivity.onCreate(MainActivity.java:17)
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.app.Activity.performCreate(Activity.java:5104)
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-06 03:22:20.017: E/AndroidRuntime(908):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-06 03:22:20.017: E/AndroidRuntime(908):  ... 11 more

0 个答案:

没有答案