Android locationManager 空指针异常? (如何传递上下文?)
我这里有问题,我无法让它工作...... 我想获取最新的 GPS 位置,但我得到的只是空指针异常。 它适用于第一类 GPSActivity,但不适用于第二类 SmsReceiver。
我读到这可能是因为我必须将上下文传递给第二类,但我不知道如何......请帮忙!
下面是代码:
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();
}
}
}
我稍微删除了代码)
现在,我可以调用 showCurrentLocation,它会显示经度和纬度值。
我该如何在这里执行此操作:
SmsReceive.class
(收到短信时,发送 GPS 坐标)
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();
}
}
}
谢谢!
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!
Here comes the 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();
}
}
}
(I stripped the code a bit)
Now, I can call showCurrentLocation and it showes me the long and lat values.
How can I do this here:
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();
}
}
}
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 smsReceiver 类中声明一个新的 locationManager 并使用以下行
declare a new locationManager in the smsReceiver class and use the following lines
创建扩展服务的 One 类,并在该类中实现 GPS 任务,因为在 Activity 中,它将停止/暂停/销毁,而您无法每次都执行。服务将在后台运行,以便您可以随时获取 GPS 坐标,并将位置对象定义为公共静态,以便您可以在应用程序中的任何位置使用。
Create the One class which extends the Service and into this implement the GPS task because in Activity which will stop/pause/destroy and you can't get every time. Service will run into background so you can get the GPS co-ordinate at any time any and define the location object as public static so you can use anywhere into your application.