我的应用程序中有一个广播接收器类。应用程序应该在接收消息时开始播放音频但是我的应用程序在收到消息后立即崩溃。这是SmsReceiver类的代码:
public class SmsReceiver extends BroadcastReceiver {
public static final String SMS_EXTRA_NAME = "pdus";
MediaPlayer mPlay = new MediaPlayer();
public void onReceive(Context context, Intent intent) {
// Get the SMS map from Intent
Bundle extras = intent.getExtras();
String body = "";
Context a = null;
mPlay.create(a, R.raw.sample);
if (extras != null) {
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);
for (int i = 0; i < smsExtra.length; ++i) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
body = sms.getMessageBody().toString();
if (body.equalsIgnoreCase("xxx")) {
mPlay.start();
this.abortBroadcast();
}
}
}
}
public void onKeyDown() {
mPlay.stop();
}
public void onDestroy() {
mPlay.stop();
}
}
logcat的:
I/PackageManager( 174): /data/app/com.test.example-2.apk changed; collecting certs
I/PackageManager( 174): /data/app/com.test.example-2.apk changed; unpacking
I/ActivityManager( 174): Start proc com.test.example for broadcast com.test.example/.SmsReceiver: pid=1043 uid=10083 gids={}
E/AndroidRuntime( 1043): java.lang.RuntimeException: Unable to start receiver com.test.example.SmsReceiver: java.lang.NullPointerException
E/AndroidRuntime( 1043): at com.test.example.SmsReceiver.onReceive(SmsReceiver.java:20)
I/ActivityManager( 174): Process com.test.example (pid 1043) has died.
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.example"
android:versionCode="1"
android:versionName="1.0">
<uses-permission
android:name="android.permission.WRITE_SMS"/>
<uses-permission
android:name="android.permission.READ_SMS"/>
<uses-permission
android:name="android.permission.RECEIVE_SMS"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="11"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:label="@string/app_name"
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name=".SmsReceiver" android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
有人知道如何解决这个问题吗? 任何帮助,将不胜感激!
答案 0 :(得分:1)
java.lang.RuntimeException: Unable to start receiver com.test.example.SmsReceiver: java.lang.NullPointerException
E/AndroidRuntime( 1043): at com.test.example.SmsReceiver.onReceive(SmsReceiver.java:20)
那里有一个NullPointer。在SmsReceiver.java的第20行会发生什么?
mPlay是否可能为空?
答案 1 :(得分:1)
改变
Context a = null;
mPlay.create(a, R.raw.sample);
待
mPlay.create(context, R.raw.sample);
答案 2 :(得分:0)
您的上下文为空,请参阅:
Context a = null;
mPlay.create(a, R.raw.sample);
你应该这样做:
Context a = context;
mPlay.create(a, R.raw.sample);
拥抱。
答案 3 :(得分:0)
我看到的第一个潜在问题是
Context a = null;
这应该是
Context a = context;
您传递了context
,因此没有理由将其null
。
答案 4 :(得分:0)
您正在使用某些对象引用而未在SmsReceiver.java中的第20行初始化。初始化。
mPlay.create(a, R.raw.sample);
在上面的行中,您传递的是as null。做到这一点
mPlay.create(this, R.raw.sample);