广播接收器空指针 Android
Broadcast Receiver null pointer Android
我不断收到以下错误:
> java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.BroadcastReceiver.onReceive(android.content.Context, android.content.Intent)' on a null object reference
当我尝试从我的服务接收广播时 class。
服务:
@Override
public void onDestroy(){
super.onDestroy();
Intent intent = new Intent("UpdateLocation");
intent.putExtra("Location",journ);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
这是将自定义对象 (Journ) 发送到我的主程序的广播的代码 activity。
主要:
@Override
protected void onCreate(Bundle savedInstanceState)
LocalBroadcastManager.getInstance(this).registerReceiver(
mReceiver, new IntentFilter("UpdateLocation"));
//Listen for service to send location data
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
temp= intent.getExtras().getParcelable("Location");
}
};
}
@Override
public void onPause() {
if (!tracking) {
finish();
}
//Run service in background to keep track of location
startService(new Intent(this, locationService.class));
super.onPause();
}
@Override
public void onResume() {
if (!tracking) {
return;
}
if (mGoogleApiClient != null) {
//Reconnect to google maps
mGoogleApiClient.reconnect();
}
stopService(new Intent(this, locationService.class));
super.onResume();
}
我不知道该怎么做,我正在尝试从我的服务 class 传递对象,该服务在我的应用程序处于后台时运行,当它恢复时服务应该停止并发送它收集到我的主要 activity.
的数据
然而,这不起作用,有什么想法吗?
以防人们怀疑我的 on create 方法确实包含更多代码,但我认为没有必要包含。
在onCreate mReciever 是一个空对象(如果你之前没有分配它),所以你应该在注册接收器之前分配它。
更改此部分:
if (mReceiver == null) {
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
temp= intent.getExtras().getParcelable("Location");
}
};
}
//Listen for service to send location data
LocalBroadcastManager.getInstance(this)
.registerReceiver(mReceiver, new IntentFilter("UpdateLocation"));
我不断收到以下错误:
> java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.BroadcastReceiver.onReceive(android.content.Context, android.content.Intent)' on a null object reference
当我尝试从我的服务接收广播时 class。
服务:
@Override
public void onDestroy(){
super.onDestroy();
Intent intent = new Intent("UpdateLocation");
intent.putExtra("Location",journ);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
这是将自定义对象 (Journ) 发送到我的主程序的广播的代码 activity。
主要:
@Override
protected void onCreate(Bundle savedInstanceState)
LocalBroadcastManager.getInstance(this).registerReceiver(
mReceiver, new IntentFilter("UpdateLocation"));
//Listen for service to send location data
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
temp= intent.getExtras().getParcelable("Location");
}
};
}
@Override
public void onPause() {
if (!tracking) {
finish();
}
//Run service in background to keep track of location
startService(new Intent(this, locationService.class));
super.onPause();
}
@Override
public void onResume() {
if (!tracking) {
return;
}
if (mGoogleApiClient != null) {
//Reconnect to google maps
mGoogleApiClient.reconnect();
}
stopService(new Intent(this, locationService.class));
super.onResume();
}
我不知道该怎么做,我正在尝试从我的服务 class 传递对象,该服务在我的应用程序处于后台时运行,当它恢复时服务应该停止并发送它收集到我的主要 activity.
的数据然而,这不起作用,有什么想法吗?
以防人们怀疑我的 on create 方法确实包含更多代码,但我认为没有必要包含。
在onCreate mReciever 是一个空对象(如果你之前没有分配它),所以你应该在注册接收器之前分配它。 更改此部分:
if (mReceiver == null) {
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
temp= intent.getExtras().getParcelable("Location");
}
};
}
//Listen for service to send location data
LocalBroadcastManager.getInstance(this)
.registerReceiver(mReceiver, new IntentFilter("UpdateLocation"));