我正在编写一个自定义应用程序,并且为了更好地维护代码,我想将它们中的不同功能作为不同的类分开。android将位置方法传递给另一个类// nullpointexception
目前,我有类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();}
}
}
你需要看所产生的堆栈跟踪(logcat的)。它会告诉你代码的违规行。 – techiServices
我想知道,如果这becase的GPSLoc也延伸活动,所以在这里使用意图必须? – user2557930
任何原因GPS的东西在不同的活动,以实际显示的数据吗? – techiServices