0

I want to call in MainActivity.java call sendSMS.java i'm beginner and i just copy paste code from this site and google... but when i want to create separately SendSMS class and call method sendSMS(message,phone) i got error...

and when i put sendSMS() to MainActivity works just fine...

MainActivity {
//some code here
SendSMS some = new SendSMS();
some.sendSMS(message,phone);
//some code here
}

and

public class SendSMS extends AppCompatActivity  {
public void sendSMS(String message, String phone) {

    String phoneNo = phone;
    String message = message;

    try {
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);

        registerReceiver(new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context arg0, Intent arg1)
            {
                int resultCode = getResultCode();
                switch (resultCode)
                {
                    case Activity.RESULT_OK: {
                        Toast.makeText(getBaseContext(), "Message SENT!",Toast.LENGTH_LONG).show();
                    }
                    break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:       Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:         Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:        Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_LONG).show();
                        break;
                }
            }
        }, new IntentFilter("SMS_SENT"));
        SmsManager smsMgr = SmsManager.getDefault();
        smsMgr.sendTextMessage(phoneNo, null, message, sentPI, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

and i have this error:

W/System.err: java.lang.NullPointerException
W/System.err:     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
W/System.err:     at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:478)
W/System.err:     at android.app.PendingIntent.getBroadcast(PendingIntent.java:467)
W/System.err:     at com.example.sendersms.SendSMS.sendSMS(SendSMS.java:84)
W/System.err:     at com.example.sendersms.MainActivity.takeJSON(MainActivity.java:216)
W/System.err:     at com.example.sendersms.MainActivity$1$1.run(MainActivity.java:90)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:136)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5001)
W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
W/System.err:     at dalvik.system.NativeStart.main(Native Method)
user1721620
  • 505
  • 1
  • 6
  • 13

3 Answers3

1

Never instantiate activities with new. The instance won't be initialized as an activity and not usable for anything you'd want an activity for.

To fix your code:

  • Remove the extends AppCompatActivity

  • Since your sendSMS() method needs a Context, pass it as an argument:

    public void sendSMS(Context context, String message, String phone) { ...
    
  • In your caller, pass in the Context. For example, in a valid Activity:

    sendSMS(this, 
    
  • Use the param where you need the Context, e.g.

    PendingIntent.getBroadcast(context, ...
    
laalto
  • 150,114
  • 66
  • 286
  • 303
  • when i remove extends AppCompatActivity i have another error on methos sendSMS: "cannot resolve method getBaseContext()" and "cannot resolve method registerReceiver(anonymous.android.content.BroadcastReceiver,android.content.IntentFilter) – user1721620 Nov 25 '15 at 21:04
  • Yes. Use the `context` param in place of `getBaseContext()` and so on. – laalto Nov 25 '15 at 21:05
  • can i pass somehow context when i make SendSMS some = new SendSMS(); Like: SendSMS some = new SendSMS(getApplicationContext());? And in SendSMS class some constructor to create context? i don't know value of context in MainActivity.java and still have error for "registerReceiver" – user1721620 Nov 25 '15 at 21:32
  • 1
    You can also pass it in as constructor argument and store in a member variable. Note that `Activity` is-a `Context`. – laalto Nov 26 '15 at 05:45
  • can you write me that code? bacause i don't know how to do that? – user1721620 Nov 26 '15 at 14:12
0

SendSMS is an Activity since you extend AppCompatActivity. You have to register Activity classes in your AndroidManifest.xml file. You shouldn't instantiate an Activity. You should talk to it via Intents. Android instantiates the Activity so it can wire up the Context and other dependencies. Your NullPointerException is happening because you instantiated SendSMS and none of the dependencies of an Activity is being wired up by Android.

Josh Chappelle
  • 1,558
  • 2
  • 15
  • 37
0

Laalto help me to find solution for my problem... complete answer is

in class MainActivity

SendSMS smsclass = new SendSMS(this);
smsclass.method();

in class SendSMS

private Context mContext;
public SendSMS() {
    super();
}
protected SendSMS(Context context){
    mContext = context;
}

i don't know how this working but that works for me

once more thanks to laalto!

user1721620
  • 505
  • 1
  • 6
  • 13