我想将经度和纬度从类onLocationChanged中的GPSTracker方法发送到GPSPrincipal(这是我的主要活动),并激活一个显示经度和纬度的吐司。

我在将数据从GPSTracker发送到GPSPrincipal的broadcastreciver遇到问题,同时,应用崩溃了,它显示了以下错误:


  致命异常:位于以下位置的主要java.lang.NullPointerException
  android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:344)
  在
  co.edu.javeriana.introcm.gpsexample.GPSTracker.onLocationChanged(GPSTracker.java:192)


192行是:

sendBroadcast(broadcastIntent);


onLocationChangedGPSTracker的最后一行

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


GPS追踪器:

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>

最佳答案

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

07-24 15:44