-1

我正在编写一个自定义应用程序,我想将其中的不同功能像不同的类一样分开,以便更好地维护代码。

目前,我有班级MainActivity和班级GPSLoc;有一个按钮MainActivity应该通过调用 Class 中的方法来显示纬度和经度GPSLoc,但不幸的是,一旦我点击按钮,它就会抛出NullPointException,这是GPSLoc类,我已经独立测试了这些方法,它工作正常.

 public void onClick(View v) {
    GPSLoc Test1=new GPSLoc();
    Test1.showCoordinate();
    }
});

这里也是 LogCat

07-07 19:04:24.730: E/AndroidRuntime(830): FATAL EXCEPTION: main
07-07 19:04:24.730: E/AndroidRuntime(830): java.lang.NullPointerException
07-07 19:04:24.730: E/AndroidRuntime(830):  at com.example.testnewbutton.GPSLoc.showCoordinate(GPSLoc.java:122)
07-07 19:04:24.730: E/AndroidRuntime(830):  at com.example.testnewbutton.MainActivity$1.onClick(MainActivity.java:53)
07-07 19:04:24.730: E/AndroidRuntime(830):  at android.view.View.performClick   (View.java:4204)
07-07 19:04:24.730: E/AndroidRuntime(830):  at android.view.View$PerformClick.run(View.java:17355)

这是 ShowCoordinate 方法

   public void showCoordinate ()



   {

    LocationManager manager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);

       LocationListener ll=new MyLocationListener();
       manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);




       Location location=manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);



       if (location != null) {
           String message = String.format(
                   "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                   location.getLongitude(), location.getLatitude()

                   );

           {  Toast.makeText(mContext, message,Toast.LENGTH_LONG).show();}


       }









   }
4

1 回答 1

0

使用下面的代码。公共类 LocationListenerClass

{ 私有静态 LocationListenerClass 实例;

私有静态上下文上下文;私人位置管理器我的位置管理器;私人位置监听器我的位置监听器;私人静态双纬度= 0d;私人静态双经度= 0d;

public static LocationListenerClass getInstance(Context context) {
    LocationListenerClass.context = context;
    if (null == instance) {
        instance = new LocationListenerClass();
    }
    return instance;
}

public void getCurrentLocation() {
    try {
        myLocationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        myLocationListener = new MyLocationListener();
        myLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 30000, 100,
                myLocationListener);

        Location location;
        location = myLocationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location == null) {
            myLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 30000, 100,
                    myLocationListener);
            location = myLocationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }

        if (location != null) {
            try {
                latitude = location.getLatitude();
                Data.CURENT_LATITUDE = latitude;
                Log.v(ConstantLib.LOG, " latitude : "
                        + Data.CURENT_LATITUDE);
                longitude = location.getLongitude();
                Data.CURENT_LONGITUDE = longitude;
                Log.v(ConstantLib.LOG, " longitude : "
                        + Data.CURENT_LONGITUDE);

            } catch (Exception e) {
            }

        }
    } catch (Exception e) {
    }

}

public void removeLocationUpdates() {
    try {
        if (myLocationManager != null) {
            myLocationManager.removeUpdates(myLocationListener);
        }
    } catch (Exception e) {
    }
}

class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {

        try {
            if (location != null) {
                Data.CURENT_LATITUDE = location.getLatitude();
                Log.v(ConstantLib.LOG, "LOCATION CHANGED" + " latitude : "
                        + Data.CURENT_LATITUDE);
                longitude = location.getLongitude();
                Data.CURENT_LONGITUDE = location.getLongitude();
                Log.v(ConstantLib.LOG, "LOCATION CHANGED" + " longitude : "
                        + Data.CURENT_LONGITUDE);
            }
        } catch (Exception e) {

        }

    }

    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
    }
}

}

于 2013-07-08T09:22:23.833 回答