Bono88
Bono88

Reputation: 1

Media Button Receiver android

I have a problem with the following code for get the action of the media button.

Class MediaButtonIntentReceiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;

public class MediaButtonIntentReceiver extends BroadcastReceiver {

public MediaButtonIntentReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent intent) {
    String intentAction = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        KeyEvent event = (KeyEvent) intent
                .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        int action = event.getAction();
        if (action == KeyEvent.ACTION_DOWN) {
            Log.i("ok", "media button pressed");
        }

    }

    if (isOrderedBroadcast()) {
        abortBroadcast();
    }
}
}

In my main I have this:

MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
mediaFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
registerReceiver(mMediaButtonReceiver, mediaFilter);

and this is the receiver on my manifest

<receiver android:name="ch.bono88.Ederly.MediaButtonIntentReceiver">
     <intent-filter>
          <action android:name="android.intent.action.MEDIA_BUTTON"/>
     </intent-filter>
</receiver>

So when I try to execute my application I got this exception

java.lang.RuntimeException: Unable to start receiver ch.bono88.Ederly.MediaButtonIntentReceiver: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at ch.bono88.Ederly.MediaButtonIntentReceiver.onReceive(MediaButtonIntentReceiver.java:26)

Upvotes: 0

Views: 4726

Answers (1)

Don Cesar D&#39;Bazar
Don Cesar D&#39;Bazar

Reputation: 329

You need to check which exact line 26 in your MediaButtonIntentReceiver source. I can conclude indirectly only that you need to check for null for event, since it can be null.

Upvotes: 1

Related Questions