提前感谢您的帮助
我正在尝试在位置更新方法中显示经度纬度,但是获取空指针异常。 我的主要活动代码是
public class LocationStat extends Activity implements LocationListener{
double logi;
double lat;
long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Millisecon
Location loc;
LocationManager manager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
manager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new LocationStat()
);
Toast.makeText(this,"activity created...",Toast.LENGTH_LONG).show();
loc=manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
String s;
if(loc!=null)
{
logi=loc.getLongitude();
lat=loc.getLatitude();
s="Lat:" + lat + "\nLong:" + logi;
}
else
{
s ="no location found";
}
Toast.makeText(LocationStat.this,"Your Current Position is:\n" +
s,Toast.LENGTH_LONG).show();
webcall(logi,lat);
//getLoc();
}
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
if(!message.equals(null))
{
Toast.makeText(this, message,Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "result is null",Toast.LENGTH_LONG).show();
}
//getLoc();
//webcall(location.getLongitude(),location.getLatitude());
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(LocationStat.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(LocationStat.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(LocationStat.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
和清单中的权限如下
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
和logcat如下
08-27 07:10:33.079: E/AndroidRuntime(485): FATAL EXCEPTION: main
08-27 07:10:33.079: E/AndroidRuntime(485): java.lang.NullPointerException08-27 07:10:33.079: E/AndroidRuntime(485): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
08-27 07:10:33.079: E/AndroidRuntime(485): at android.widget.Toast.<init> (Toast.java:89)
08-27 07:10:33.079: E/AndroidRuntime(485): at android.widget.Toast.makeText(Toast.java:231)
08-27 07:10:33.079: E/AndroidRuntime(485): at com.android.trace.LocationStat.onLocationChanged(LocationStat.java:83)
08-27 07:10:33.079: E/AndroidRuntime(485): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191)
08-27 07:10:33.079: E/AndroidRuntime(485): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
08-27 07:10:33.079: E/AndroidRuntime(485): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
08-27 07:10:33.079: E/AndroidRuntime(485): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 07:10:33.079: E/AndroidRuntime(485): at android.os.Looper.loop(Looper.java:123)
08-27 07:10:33.079: E/AndroidRuntime(485): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-27 07:10:33.079: E/AndroidRuntime(485): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 07:10:33.079: E/AndroidRuntime(485): at java.lang.reflect.Method.invoke(Method.java:521)
08-27 07:10:33.079: E/AndroidRuntime(485): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-27 07:10:33.079: E/AndroidRuntime(485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-27 07:10:33.079: E/AndroidRuntime(485): at dalvik.system.NativeStart.main(Native Method)
08-27 07:10:44.602: I/Process(485): Sending signal. PID: 485 SIG: 9
答案 0 :(得分:1)
!message.equals(null)
即使消息实际为null,也为false。使用
message != null
测试实际的无效或
TextUtils.isEmpty(message)
同时测试空虚或无效
答案 1 :(得分:0)
Hy选择GPS提供商白度标准getBestProvider,即使您没有标准。 我得到了相同的错误,但这解决了。