3

Here is my source code and it keeps force closing everytime I run it...

public class MainActivity extends Activity {
    private static String content;
    private static String phone;
    private String number;
    private String message;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                //---get the SMS message passed in---
                Bundle bundle = intent.getExtras();        
                SmsMessage[] msgs = null;

                if (bundle != null)
                {
                    number = "";
                    message = "";
                    //---retrieve the SMS message received---
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];            
                    for (int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                        number = msgs[i].getOriginatingAddress();                       
                        message = msgs[i].getMessageBody();
                    }
                    //---display the new SMS message--- 
                    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                    SendMe();
                }
            }
        }, null);
    }

    public void SendMe(){
        PendingIntent pi =  PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);  
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(number, null, message, pi, null);
    }
}

Here is the logcat I get...

06-28 14:39:00.331: ERROR/AndroidRuntime(396): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ftt.autospond/com.ftt.autospond.MainActivity}: java.lang.NullPointerException
06-28 14:39:00.331: ERROR/AndroidRuntime(396):     at android.app.ActivityManagerProxy.registerReceiver(ActivityManagerNative.java:1504)
06-28 14:39:00.331: ERROR/AndroidRuntime(396):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:807)
06-28 14:39:00.331: ERROR/AndroidRuntime(396):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
06-28 14:39:00.331: ERROR/AndroidRuntime(396):     at com.ftt.autospond.MainActivity.onCreate(MainActivity.java:29)
Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
theITRanger22
  • 184
  • 6
  • 19

3 Answers3

4

You need to move your receiver outside the onCreate. something like -

public class MainActivity extends Activity {
private static String content;
private static String phone;
private String number;
private String message;

private  BroadcastReceiver receiver =  new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;

        if (bundle != null)
        {
            number = "";
             message = "";
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                number = msgs[i].getOriginatingAddress();                     

               message = msgs[i].getMessageBody();


            }
            //---display the new SMS message--- 
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            SendMe();
    }
    }


  };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    IntentFilter filter = new IntentFilter();
    filter.addAction(YOUR_SMS_ACTION);
    this.registerReceiver(this.receiver, filter);
    setContentView(R.layout.main);
    }


public void SendMe(){


    PendingIntent pi =  PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);  
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(number, null, message, pi, null);

}
}
Suchi
  • 9,989
  • 23
  • 68
  • 112
  • @Suchi okay i think that will do it. Do i need to call anything in my onCreate for it? – theITRanger22 Jun 28 '11 at 15:19
  • I have added the registerReceiver call in the onCreate. That is working for me. Let me know if it works out! – Suchi Jun 28 '11 at 15:21
  • 1
    @Suchi =( Im still getting a force close after that. Maybe its something im doing wrong – theITRanger22 Jun 28 '11 at 15:30
  • @Suchi i just pasted my code that force closes in the bottom of your answer. Thanks for all your help. – theITRanger22 Jun 28 '11 at 15:33
  • try now.. there was some conflict when we both were trying to edit together – Suchi Jun 28 '11 at 15:37
  • @Suchi Okay so it doesnt force close now until i run the code and try to send a text message to the emulator and then it force closes. – theITRanger22 Jun 28 '11 at 15:44
  • @Suchi here is the exemption that really stood out to me 06-28 16:51:43.972: ERROR/AndroidRuntime(712): java.lang.RuntimeException: Unable to instantiate receiver com.ftt.autospond.MainActivity: java.lang.ClassCastException: com.ftt.autospond.MainActivity – theITRanger22 Jun 28 '11 at 16:52
0

The force close is likely happening because you are managing the UI from within your broadcast receiver. There's a 10-second limit on a BR's onReceive before it is forced closed.

To solve, use an Activity component to generate your Toast.

Dana
  • 354
  • 2
  • 9
0

I am a little confused here. It seems that you want to register aBroadcastReceiver for the "SMS_RECEIVED" IntentFilter but the filter has not been declared anywhere in the code as far as I can see.

Try replacing the null at the end of registerReceiver {} to new IntentFilter("SMS_RECEIVED")); to see if it works. Maybe its the reason why you are getting a null pointer exception.

i.e. } } }, null);

to } } }, new IntentFilter("SMS_RECEIVED"));

Dave
  • 133
  • 8