我正在编写一个自定义应用程序,我希望将不同类中的不同函数分开,以便更好地进行代码维护。
目前,我有班级MainActivity
和班级GPSLoc
; MainActivity
中有一个按钮,它应该通过调用类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();}
}
}
答案 0 :(得分:0)
使用以下代码。 公共类LocationListenerClass
{ private static LocationListenerClass instance;
私有静态Context上下文; private LocationManager myLocationManager; private LocationListener myLocationListener; private static Double latitude = 0d; private static Double longitude = 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
}
}
}