的Android的LocationManager空指针异常? (如何通过上下文?) [英] Android locationManager Null Pointer Exception? (how to pass the context?)
问题描述
我在这里有一个问题,我不能让它工作...
我想获得最新的GPS位置,但我得到的是一个空指针异常。
它的工作原理与第一类GPSActivity但不与第二SmsReceiver
I have a problem here and I cant get it to work... I want to get the latest gps positions but all i get is an null pointer exception. It works with the first class GPSActivity but not with the 2nd SmsReceiver.
我读到这也许是因为我有上下文传递给第二类,但我不知道如何......请大家帮忙!
I read that this is maybe because I have to pass the Context to the 2nd class but I dont know how... please help!
下面来了code:
GPSActivity.class
:
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class GPSActivity extends Activity {
long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
long MINIMUM_TIME_BETWEEN_UPDATES = 10000; // in Milliseconds
LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.getAllProviders();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener());
}
public void showCurrentLocation() {
Location location =
locationManager.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(GPSActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
}
(我扯下了code位)
(I stripped the code a bit)
现在,我可以调用showCurrentLocation并showes我长期和纬度值。
Now, I can call showCurrentLocation and it showes me the long and lat values.
我怎样才能做到这一点的位置:
How can I do this here:
SmsReceive.class
(当接收到一个短信,发送GPS坐标)
SmsReceive.class
(when a sms is received, send the gps coordinates)
import ...
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
GPSActivity myGPS;
myGPS = new GPSActivity();
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
//HERE I GET THE NULL POINTER EXCEPTION
Location loc =
myGPS.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double varLong = loc.getLongitude();
double varLat = loc.getLatitude();
String locationData = "LONG: " + varLong+ " LAT: " + varLat;
Toast.makeText(context, locationData, Toast.LENGTH_SHORT).show();
}
}
}
谢谢!
推荐答案
在smsReceiver类中声明一个新的LocationManager,并使用以下行
declare a new locationManager in the smsReceiver class and use the following lines
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
这篇关于的Android的LocationManager空指针异常? (如何通过上下文?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!