Android 多次调用广播接收器类,以不同的意图显示;空指针错误“;

Android 多次调用广播接收器类,以不同的意图显示;空指针错误“;,android,broadcastreceiver,Android,Broadcastreceiver,我的应用程序有一个BroadcastReceiving和mainactivity类。 从mainactivity我打了三次电话给广播接收班,目的不同,地点不同 我想要的是当发现不同的意图时,在广播接收类中执行不同的代码 eg: when intent contains string it execute a() function when intent is int it executes b() function 但当我在接收时提取数据时,它会使我的应用程序崩溃,显示“空指针异

我的应用程序有一个BroadcastReceiving和mainactivity类。 从mainactivity我打了三次电话给广播接收班,目的不同,地点不同

我想要的是当发现不同的意图时,在广播接收类中执行不同的代码

eg: 
   when intent contains string it execute a() function
   when intent is int it executes b() function

但当我在接收时提取数据时,它会使我的应用程序崩溃,显示“空指针异常”如何解决此问题

为每个不同的操作定义不同的操作字符串。为每个
目的设置适当的操作
,并在
广播接收器的onReceive中检查操作

/* when sending a broadcast */
Intent intent = new Intent("action_a");
context.sendBroadcast(intent);

/* in the BroadcastReceiver */
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    if (TextUtils.isEmpty(action)) {
        // intent didn't have an action
        return;
    }

    if ("action_a".equals(action)) {
        // do function A
    } else if ("action_b".equals(action)) {
        // do function B
    } // and so on
}
在AndroidManifest中,添加以下内容作为
标记的子元素:

<intent-filter>
    <action name="action_a" />
    <action name="action_b" />
    <!-- and so on -->
</intent-filter>


您的代码和logcat错误在哪里?您能在这里显示您的代码吗?确保您也注册了所有三个意图,如果意图不为空,请签入onreReceive()。