0

提前感谢您的帮助

我试图在位置更新方法中显示经度和纬度,但出现空指针异常。我的主要活动代码是

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();
}

manifest中获取的权限如下

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

2 回答 2

1
!message.equals(null)

即使消息实际上为空,也是假的。使用任一

message != null

测试实际无效或

TextUtils.isEmpty(message)

同时检验空虚或空虚

于 2012-08-27T08:04:30.560 回答
0

Hy 选择 GPS Provider whit 标准 getBestProvider,即使你没有标准。我得到了同样的错误,但这已经解决了。

于 2012-08-27T08:01:49.310 回答