Android onLocationChanged GPSTracker崩溃

时间:2016-02-28 02:17:27

标签: android android-intent broadcastreceiver

我想将经度和纬度从我的onLocationChanged方法发送到GPSTrackerGPSPrincipal(这是我的主要活动)并激活显示经度和纬度的吐司。

我在使用broadcastreciver将数据从GPSTracker发送到GPSPrincipal时遇到问题,并且应用程序崩溃并显示以下错误:

  

FATAL EXCEPTION:主java.lang.NullPointerException at   android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:344)   在   co.edu.javeriana.introcm.gpsexample.GPSTracker.onLocationChanged(GPSTracker.java:192)

第192行是:

sendBroadcast(broadcastIntent);

onLocationChanged

GPSTracker的最后一行

GPSPrincipal(主要活动):

public class GPSPrincipal extends Activity {
    GPSTracker gps;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gpsprincipal);
    }

    public void loca(View arg0) {
        gps = new GPSTracker(GPSPrincipal.this);
        if(gps.canGetLocation()){
            TextView latitud = (TextView) findViewById(R.id.showLatitud);
            TextView longitud = (TextView) findViewById(R.id.showLongitud);
            latitud.setText(String.valueOf(gps.getLatitude()));
            longitud.setText(String.valueOf(gps.getLongitude()));}
        else {
            gps.showSettingsAlert();}
    }

    public void cambio () {
        BroadcastReceiver receiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                Toast.makeText(context, "Located: - \nLat: " + intent.getExtras().get("latitude") + "\nLong: " + intent.getExtras().get("longitude"), Toast.LENGTH_LONG).show();}};
        registerReceiver(receiver, new IntentFilter("com.example.broadcast.gps.location_change"));
    }    }

GPSTracker:

public class GPSTracker extends Service implements LocationListener {
    private final Context mContext;
    private Location location;
    private double latitude; // latitud
    private double longitude; // longitud

    protected LocationManager locationManager;

    public GPSTracker(Context context) {
        this.mContext = context; //Obtener el contexto
        getLocation();
   }

    public void onLocationChanged(Location location) {
        Intent broadcastIntent = new Intent("com.example.broadcast.gps.location_change");
        broadcastIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        broadcastIntent.putExtra("latitude", latitude);
        broadcastIntent.putExtra("longitude", longitude);
        sendBroadcast(broadcastIntent);
    }

清单:

package="co.edu.javeriana.introcm.gpsexample"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".GPSPrincipal"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

1 个答案:

答案 0 :(得分:1)

在ContextWrapper类中调用sendBroadcast()时,您的上下文可能是空的。也许你应该尝试使用mContext.sendBroadcast,看看是否有所作为。