gpt4 book ai didi

java - android另一个类sendSMS并在Mainactivity中调用错误

转载 作者:行者123 更新时间:2023-12-01 10:50:46 25 4
gpt4 key购买 nike

我想在MainActivity.java中调用sendSMS.java我是初学者,我只是从这个网站和谷歌复制粘贴代码......但是当我想单独创建 SendSMS 类并调用方法 sendSMS(message,phone) 时,我收到错误...

当我将 sendSMS() 放入 MainActivity 时效果很好......

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

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();
}
}

我有这个错误:

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)

最佳答案

切勿使用new实例化 Activity 。该实例不会被初始化为 Activity ,并且不能用于您想要 Activity 的任何用途。

要修复您的代码:

  • 删除扩展AppCompatActivity

  • 由于您的 sendSMS() 方法需要 Context,因此请将其作为参数传递:

    public void sendSMS(Context context, String message, String phone) { ...
  • 在调用者中,传入上下文。例如,在有效的 Activity 中:

    sendSMS(this, 
  • 在需要上下文的地方使用参数,例如

    PendingIntent.getBroadcast(context, ...

关于java - android另一个类sendSMS并在Mainactivity中调用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33925898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com