开发者

Intent.getAction() is throwing Null + Android

I have written BroadcastReceiver for sort of Alarm application. In onReceive method I am reading Intent.getAction(). It is running well in all of the versions *except in SDK version 1.5 where it throws null pointer exception*. I am setting the action in another activity where I am calling broadcastReceiver. Please help me out with this. Below is the code snippet for both receiver and activity classes,

ProfileActivity.java


public static final String STARTALARMACTION = "android.intent.开发者_如何学Cdriodaceapps.action.STARTPROFILE";

public static final String STOPALARMACTION = "android.intent.driodaceapps.action.STOPPROFILE";

AlarmManager alarmMan = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Intent startIntent = new Intent(this,AlarmReceiver.class);

startIntent.setAction(STARTALARMACTION);

Intent stopIntent = new Intent(this,AlarmReceiver.class);

stopIntent.setAction(STOPALARMACTION+intentposition);

AlarmReceiver.java

@Override
public void onReceive(Context context,Intent intent){

    String intentAction = intent.getAction(); 

    Log.d(TAG,"Intent:"+intentAction); **//throwing null**

After getting the error I tried my luck by giving the actions in of .manifest file. But no use.

Please help me.

Thanks.


From the docs for BroadcastReceiver.onReceive()...

The Intent filters used in registerReceiver(BroadcastReceiver, IntentFilter) and in application manifests are not guaranteed to be exclusive.

For this reason, onReceive() implementations should respond only to known actions, ignoring any unexpected Intents that they may receive.

I don't fully understand the implications of either of the statements above but it seems the second of the two is that an onReceive() handler should be written to cope with the unexpected.

Remember that Intents don't need to have an associated 'action' (they can be used simply to pass data between differnt entities). Intent.getAction() will return null if the Intent has no action set and attempting to Log intentAction will call an NPE.

Are you sure the Intent is one of your own and not another which has found it's way to your receiver? I'm not familiar with v1.5 but it's possible things may have worked a different way - unlikely perhaps but possible.

I think the simple thing to try is to simply test intentAction to see if it is null and ignore it if it is (i.e., exit the onReceive() handler assuming the intent isn't one of your own). Worth a try.


Following on from @Squonk's excellent reply, I placed the following code in my BroadcastReciever's onReceive:

if (intent != null) {
    if(intent.getAction() != null){
        Log.i(TAG, "Intent: " + intent.getAction());
    }else{
        Log.i(TAG, "Intent: !null, Action: null");
    }
}else{
    Log.i(TAG, "Intent: null");
}

My reciever is set to fire every 60 seconds but something else is calling it:

12-03 08:42:05.566 Intent: WEEKLY_CHECK
12-03 08:42:32.990 Intent: !null, Action: null
12-03 08:42:33.175 Intent: WEEKLY_CHECK
12-03 08:43:32.990 Intent: WEEKLY_CHECK
12-03 08:43:33.025 Intent: !null, Action: null
12-03 08:43:33.057 Intent: WEEKLY_CHECK
12-03 08:44:32.990 Intent: WEEKLY_CHECK
12-03 08:44:33.069 Intent: !null, Action: null
12-03 08:44:33.102 Intent: WEEKLY_CHECK

Thus confirming what's stated in the docs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜