java - Android locationManager 空指针异常? (如何传递上下文?)

标签 java android gps 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();
    }
  }
}

谢谢!

最佳答案

在 smsReceiver 类中声明一个新的 locationManager 并使用以下行

mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
 Location loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

关于java - Android locationManager 空指针异常? (如何传递上下文?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7724685/

相关文章:

java - 模拟方法没有被调用 - java

android - AsynTask 中的文件类型错误

android - System.currentTimeMillis() 在 Motorola Droid 上总是返回 0

Android View.setKeepScreenOn() - 它在 View 消失时适用吗?

android - 确定用户是否正在使用 gps 驾驶

java - 错误 : Could not find action or result No result defined for action action. 部分和结果 {"col1":"col1","col2":"col2"}

orm - Java SE 上的 openJPA

java - Datanucleus、JDO 和可执行 jar——怎么做?

Android - 文本在微调器中被推到左侧

android - 在Android后台获取用户GPS位置的最佳方法