メディアボタンのアクションを取得するための次のコードに問題があります。
クラス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();
}
}
}
私のメインにはこれがあります:
MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
mediaFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
registerReceiver(mMediaButtonReceiver, mediaFilter);
これが私のマニフェストの受信者です
<receiver android:name="ch.bono88.Ederly.MediaButtonIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
そのため、アプリケーションを実行しようとすると、この例外が発生しました
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)