我正在编写一个自定义应用程序,我想将其中的不同功能像不同的类一样分开,以便更好地维护代码。
目前,我有班级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();}
}
}