gpt4 book ai didi

Android onLocationChanged GPSTracker 崩溃

转载 作者:行者123 更新时间:2023-11-29 01:22:58 25 4
gpt4 key购买 nike

我想从 GPSTracker 类中的 onLocationChanged 方法发送经度和纬度到 GPSPrincipal(这是我的主要 Activity )和激活显示经度和纬度的 toast 。

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

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

第 192 行是:

sendBroadcast(broadcastIntent);

GPSTrackeronLocationChanged 的最后一行

GPSPrincipal(主要 Activity ):

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

全局定位系统追踪器:

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

list :

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 并查看是否有所不同。

关于Android onLocationChanged GPSTracker 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35677939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com