知道错误的原因(请参阅下面的堆栈跟踪)以及如何解决它?
在我的Activity类中,我有这些。它永远不会进入onReceive方法..为什么?
private UpdateResultReceiver mUpdateResultReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
//other stuff...
registerReceiver();
}
@Override
protected void onResume() {
super.onResume();
// Register BroadcastReceiver
registerReceiver();
}
@Override
protected void onPause() {
super.onPause();
// Unregister BroadcastReceiver
unregisterReceiver();
}
/**
* Register a BroadcastReceiver that receives a result
* from the services
*/
private void registerReceiver() {
// Create an Intent filter that handles Intents from the services
IntentFilter intentFilter =
new IntentFilter(UpdateService1.ACTION_1);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
intentFilter.addAction(UpdateService2.ACTION_2);
// Register the BroadcastReceiver.
LocalBroadcastManager.getInstance(this)
.registerReceiver(mUpdateResultReceiver,
intentFilter);
}
/**
* Unregister the receivers
*/
private void unregisterReceiver() {
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(mUpdateResultReceiver);
}
/**
* The Broadcast Receiver that registers itself to receive result
* from UploadVideoService.
*/
private class UpdateResultReceiver
extends BroadcastReceiver {
/**
* Hook method that's dispatched when the UploadService has
* uploaded the Video.
*/
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(UpdateService1.ACTION_1)) {
mFragment1.getProfileInfo();
ViewUtils.showToast(getApplicationContext(), "updated profile");
} else if(intent.getAction().equals(UpdateService2.ACTION_2)) {
mFragment2.getApptList();
ViewUtils.showToast(getApplicationContext(), "updated appts");
}
}
}
在我的UpdateService1类中,我已调试直到sendBroadcast方法并且程序崩溃:
private void sendBroadcast(){ //使用LocalBroadcastManager限制此范围 //对VideoUploadClient应用程序的意图。 LocalBroadcastManager.getInstance(本) .sendBroadcast(新意图(ACTION_1) .addCategory(Intent.CATEGORY_DEFAULT)); }
错误堆栈跟踪:
11-01 19:22:09.041: E/AndroidRuntime(26763): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.BroadcastReceiver.onReceive(android.content.Context, android.content.Intent)' on a null object reference
11-01 19:22:09.041: E/AndroidRuntime(26763): at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
11-01 19:22:09.041: E/AndroidRuntime(26763): at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
11-01 19:22:09.041: E/AndroidRuntime(26763): at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
11-01 19:22:09.041: E/AndroidRuntime(26763): at android.os.Handler.dispatchMessage(Handler.java:102)
11-01 19:22:09.041: E/AndroidRuntime(26763): at android.os.Looper.loop(Looper.java:145)
11-01 19:22:09.041: E/AndroidRuntime(26763): at android.app.ActivityThread.main(ActivityThread.java:5942)
11-01 19:22:09.041: E/AndroidRuntime(26763): at java.lang.reflect.Method.invoke(Native Method)
11-01 19:22:09.041: E/AndroidRuntime(26763): at java.lang.reflect.Method.invoke(Method.java:372)
11-01 19:22:09.041: E/AndroidRuntime(26763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
11-01 19:22:09.041: E/AndroidRuntime(26763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)