简体   繁体   中英

object oriented programming in android

I want to use a method in one of my classes in another class , my code doesn't have any error but when I run the project I get "unfortunately project * hast stopped" error. and when I copy the method instead of creating object and so on I run the program without any problem , can any one tell me what may be the problem ?

public void onClick(View arg0) {

    String p = "+989357835774";
    String m = "test";
    SendSMS sms = new SendSMS();
    sms.sms(p, m);
}


public class SendSMS extends Activity {

    public void sms (String phonno , String Message){
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, SendSMS.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phonno, "+9891100500", Message , pi, null);  
    }
}

these are my errors :

08-23 19:31:00.065: E/AndroidRuntime(9985): FATAL EXCEPTION: main
08-23 19:31:00.065: E/AndroidRuntime(9985): java.lang.NullPointerException
08-23 19:31:00.065: E/AndroidRuntime(9985):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at android.content.ComponentName.<init>(ComponentName.java:75)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at android.content.Intent.<init>(Intent.java:3655)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at com.example.finalproject3.SendSMS.sms(SendSMS.java:15)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at com.example.finalproject3.Pizza$1$2.onClick(Pizza.java:53)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at android.view.View.performClick(View.java:4101)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at android.view.View$PerformClick.run(View.java:17078)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at android.os.Handler.handleCallback(Handler.java:615)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at android.os.Looper.loop(Looper.java:155)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at android.app.ActivityThread.main(ActivityThread.java:5485)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at java.lang.reflect.Method.invoke(Method.java:511)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
08-23 19:31:00.065: E/AndroidRuntime(9985):     at dalvik.system.NativeStart.main(Native Method)

Your SendSMS class is an Activity . You should never instantiate an Activity yourself, because none of the life cycle methods will be called then. Let the system start your Activities (using intents).

If you just need a normal class that you instantiate don't inherit Activity .

To get access to methods only found in Activity or Context pass in a valid Context in your class constructor.

Assuming your onClick method is defined in an Activity:

public void onClick(View arg0) {

    String p = "+989357835774";
    String m = "test";
    SendSMS sms = new SendSMS(this);
    sms.sms(p, m);
}


public class SendSMS {

    private Context context;

    public SendSMS(Context context) {
        this.context = context;
    }

    public void sms (String phonno , String Message){
        PendingIntent pi = PendingIntent.getActivity(context, 0,
            new Intent(context, YourActivity.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phonno, "+9891100500", Message , pi, null);  
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM