android - App crashes when receives SMS in Broadcast Receiver class -


i have broadcast receiver class in app. app should start playing audio in receiving message app crashes receives message. here code of smsreceiver class :

public class smsreceiver extends broadcastreceiver {      public static final string sms_extra_name = "pdus";     mediaplayer mplay = new mediaplayer();      public void onreceive(context context, intent intent) {         // sms map intent         bundle extras = intent.getextras();         string body = "";         context = null;         mplay.create(a, r.raw.sample);         if (extras != null) {             // received sms array             object[] smsextra = (object[]) extras.get(sms_extra_name);              (int = 0; < 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 broadcast com.test.example/.smsreceiver: pid=1043 uid=10083 gids={}   e/androidruntime( 1043): java.lang.runtimeexception: unable start receiver com.test.example.smsreceiver: java.lang.nullpointerexception   e/androidruntime( 1043):  @ com.test.example.smsreceiver.onreceive(smsreceiver.java:20)   i/activitymanager(  174): process com.test.example (pid 1043) has died. 

manifest :

<?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> 

do know how fix this? appreciated!

change

context = null; mplay.create(a, r.raw.sample); 

to

mplay.create(context, r.raw.sample); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -