有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓另一个类sendSMS和调用Main活动错误

我想打电话来。java呼叫发送SMS。JAVA 我是初学者,我只是从这个网站和谷歌复制粘贴代码。。。 但当我想分别创建SendSMS类和call方法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 安卓.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
W/System.err:     at 安卓.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:478)
W/System.err:     at 安卓.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 安卓.os.Handler.handleCallback(Handler.java:733)
W/System.err:     at 安卓.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at 安卓.os.Looper.loop(Looper.java:136)
W/System.err:     at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
W/System.err:     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:601)
W/System.err:     at dalvik.system.NativeStart.main(Native Method)

共 (3) 个答案

  1. # 1 楼答案

    Laalto帮我找到解决问题的方法。。。 完整的答案是

    课堂活动

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

    课堂短信

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

    我不知道这是怎么回事,但这对我来说是有效的

    再次感谢拉阿尔托

  2. # 2 楼答案

    永远不要用new实例化活动。该实例不会被初始化为活动,并且不可用于任何您想要的活动

    要修复代码:

    • 移除extends AppCompatActivity

    • 由于sendSMS()方法需要一个Context,请将其作为参数传递:

      public void sendSMS(Context context, String message, String phone) { ...
      
    • 在呼叫者中,传入Context。例如,在有效的Activity中:

      sendSMS(this, 
      
    • 在需要^{的地方使用参数,例如

      PendingIntent.getBroadcast(context, ...
      
  3. # 3 楼答案

    SendSMS是一项活动,因为您扩展了AppCompatActivity。您必须在AndroidManifest中注册活动类。xml文件。你不应该实例化一个活动。你应该通过意向与它对话。Android会实例化该活动,以便连接上下文和其他依赖项。发生NullPointerException是因为您实例化了SendSMS,并且活动的任何依赖项都没有通过Android连接