Reputation: 3
the below code scans the incoming msg for a code (zp) and alerts if it contains the msg.. but the program closes forcefully when an sms arrives. can any plz help me to solve this? i have also aaded the sms reciver and permission in the manifest file..
public class Sms extends BroadcastReceiver {
private static final Context context = null;
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
if(message.matches("zp"))
{
Toast.makeText(context, "code received", Toast.LENGTH_SHORT).show();
}
} // end for loop
} // bundle is null
} catch (Exception e) {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
}
}
}
this is my logcat
12-14 06:44:21.464: W/Trace(12235): Unexpected value from nativeGetEnabledTags: 0 12-14 06:44:21.643: D/AndroidRuntime(12235): Shutting down VM 12-14 06:44:21.643: W/dalvikvm(12235): threadid=1: thread exiting with uncaught exception (group=0x40a70930) 12-14 06:44:21.653: E/AndroidRuntime(12235): FATAL EXCEPTION: main 12-14 06:44:21.653: E/AndroidRuntime(12235): java.lang.RuntimeException: Unable to start receiver com.christmas.Sms: java.lang.NullPointerException 12-14 06:44:21.653: E/AndroidRuntime(12235): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2383) 12-14 06:44:21.653: E/AndroidRuntime(12235): at android.app.ActivityThread.access$1500(ActivityThread.java:141) 12-14 06:44:21.653: E/AndroidRuntime(12235): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310) 12-14 06:44:21.653: E/AndroidRuntime(12235): at android.os.Handler.dispatchMessage(Handler.java:99) 12-14 06:44:21.653: E/AndroidRuntime(12235): at android.os.Looper.loop(Looper.java:137) 12-14 06:44:21.653: E/AndroidRuntime(12235): at android.app.ActivityThread.main(ActivityThread.java:5039) 12-14 06:44:21.653: E/AndroidRuntime(12235): at java.lang.reflect.Method.invokeNative(Native Method) 12-14 06:44:21.653: E/AndroidRuntime(12235): at java.lang.reflect.Method.invoke(Method.java:511) 12-14 06:44:21.653: E/AndroidRuntime(12235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 12-14 06:44:21.653: E/AndroidRuntime(12235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 12-14 06:44:21.653: E/AndroidRuntime(12235): at dalvik.system.NativeStart.main(Native Method) 12-14 06:44:21.653: E/AndroidRuntime(12235): Caused by: java.lang.NullPointerException 12-14 06:44:21.653: E/AndroidRuntime(12235): at android.widget.Toast.(Toast.java:92) 12-14 06:44:21.653: E/AndroidRuntime(12235): at android.widget.Toast.makeText(Toast.java:238) 12-14 06:44:21.653: E/AndroidRuntime(12235): at com.christmas.Sms.onReceive(Sms.java:60) 12-14 06:44:21.653: E/AndroidRuntime(12235): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2376) 12-14 06:44:21.653: E/AndroidRuntime(12235): ... 10 more
Upvotes: 0
Views: 1682
Reputation: 2097
onReceive method has paramter Context arg0 so the Toast code should be:
Toast.makeText(arg0, "code received", Toast.LENGTH_SHORT).show();
Upvotes: 1