【问题标题】:android another class sendSMS and call in Mainactivity errorandroid另一个类sendSMS并调用Mainactivity错误
【发布时间】:2016-02-28 19:08:08
【问题描述】:

我想在 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)

【问题讨论】:

    标签: java android sms send smsmanager


    【解决方案1】:

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

    在 MainActivity 类中

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

    在类发送短信中

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

    我不知道这是如何工作的,但这对我有用

    再次感谢 laalto!

    【讨论】:

      【解决方案2】:

      永远不要使用new 实例化活动。该实例不会被初始化为一个活动,并且不能用于任何你想要一个活动的东西。

      修复您的代码:

      • 删除extends AppCompatActivity

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

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

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

        PendingIntent.getBroadcast(context, ...
        

      【讨论】:

      • 当我删除扩展 AppCompatActivity 我在方法 sendSMS 上有另一个错误:“无法解析方法 getBaseContext()”和“无法解析方法 registerReceiver(anonymous.android.content.BroadcastReceiver,android.content.IntentFilter)
      • 是的。使用context 参数代替getBaseContext() 等等。
      • 当我制作 SendSMS some = new SendSMS(); 时,我能以某种方式传递上下文吗?比如:SendSMS some = new SendSMS(getApplicationContext());?而在 SendSMS 类中一些构造函数来创建上下文?我不知道 MainActivity.java 中上下文的值,并且仍然有“registerReceiver”错误
      • 您也可以将其作为构造函数参数传入并存储在成员变量中。注意Activity 是-a Context
      • 你能给我写那个代码吗?因为我不知道该怎么做?
      【解决方案3】:

      SendSMS 是一个 Activity,因为您扩展了 AppCompatActivity。您必须在 AndroidManifest.xml 文件中注册 Activity 类。你不应该实例化一个活动。您应该通过 Intents 与它交谈。 Android 实例化 Activity,以便它可以连接 Context 和其他依赖项。您的 NullPointerException 正在发生,因为您实例化了 SendSMS 并且 Android 没有连接任何 Activity 的依赖项。

      【讨论】:

      • 那么你需要阅读关于 Intents 和 Activities 的 Android 文档。
      猜你喜欢
      • 1970-01-01
      • 2017-02-03
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多