简体   繁体   English

如何自动将短信发送到预定义的电话号码

[英]How to automatically send a sms to a predefined phone number

I'm developing an app for Android that lets someone send an sms to someone and send an automatic reply to that number if they text back. 我正在开发一个适用于Android的应用,该应用可以使某人向某人发送短信,并在他们发短信时自动回复该号码。 I'm approaching this with SharedPreferences: 我正在使用SharedPreferences来解决这个问题:

public final String file = "AutoReply";
String autoReply;
public static String returned = "";
static SharedPreferences folder;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    folder = getSharedPreferences(file, 0);
    returned = folder.getString("autoReplyKey", "");
    if(returned.equals("")) {
        SharedPreferences.Editor edit = folder.edit();
        edit.putString("autoReplyKey", "Please don't respond to this phone number. Your friend borrowed my phone to text you.");
        edit.commit();
    }

Here's how someone changes the default message: 有人更改默认消息的方法如下:

LayoutInflater inflater3 = LayoutInflater.from(this);
        final View autoReply = inflater3.inflate(R.layout.auto_reply, null);
        final EditText autoreplytext = (EditText)findViewById(R.id.autoReplyText);

        final AlertDialog.Builder alert3 = new AlertDialog.Builder(this);
        alert3.setTitle("Set Auto Reply Message");
        alert3.setView(autoReply);
        alert3.setPositiveButton("Set", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String autoData = autoreplytext.getText().toString().trim();
                SharedPreferences.Editor editor = folder.edit();
                editor.putString("passwordKey", autoData);
                editor.commit();
                returned = folder.getString("AutoReplyKey", "couldn't load data");
                }
        });
        alert3.show();
        return true;

The problem is whenever I try to change the default message by using the AlertDialog, a NullPointerException error appears. 问题是,每当我尝试使用AlertDialog更改默认消息时,都会出现NullPointerException错误。 Does anyone have any idea how to fix this? 有谁知道如何解决这个问题?

Edit: 编辑:

Here's the logcat: 这是logcat:

09-09 19:43:23.145: E/AndroidRuntime(1370): FATAL EXCEPTION: main
09-09 19:43:23.145: E/AndroidRuntime(1370): java.lang.NullPointerException
09-09 19:43:23.145: E/AndroidRuntime(1370):     at       com.mshaw.avanosplus.MainActivity$16.onClick(MainActivity.java:395)
09-09 19:43:23.145: E/AndroidRuntime(1370):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
09-09 19:43:23.145: E/AndroidRuntime(1370):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 19:43:23.145: E/AndroidRuntime(1370):     at android.os.Looper.loop(Looper.java:137)
09-09 19:43:23.145: E/AndroidRuntime(1370):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-09 19:43:23.145: E/AndroidRuntime(1370):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 19:43:23.145: E/AndroidRuntime(1370):     at java.lang.reflect.Method.invoke(Method.java:511)
09-09 19:43:23.145: E/AndroidRuntime(1370):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-09 19:43:23.145: E/AndroidRuntime(1370):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-09 19:43:23.145: E/AndroidRuntime(1370):     at dalvik.system.NativeStart.main(Native Method)

As others wrote in comments, you really need to not only show the stack trace from logcat, but tell us where line 395 is. 正如其他人在评论中所写,您确实不仅需要显示logcat的堆栈跟踪,还需要告诉我们第395行在哪里。 Otherwise, we can only guess which of the many possible references contained a null. 否则,我们只能猜测许多可能引用中的哪一个包含null。

That said, I'm going to take a stab at this and guess, by elimination of other possibilities, that the culprit is autoreplytext . 就是说,我将对此采取行动,并通过消除其他可能性来猜测罪魁祸首是autoreplytext Although you didn't show us your XML for the views, I suspect that you need to change this line: 尽管您没有向我们显示视图的XML,但我怀疑您需要更改以下行:

    final EditText autoreplytext = (EditText)findViewById(R.id.autoReplyText);

to

    final EditText autoreplytext = (EditText)autoReply.findViewById(R.id.autoReplyText);

The first line searches in the content view for your main activity -- in other words, it searches within R.layout.activity_main . 第一行在内容视图中搜索您的主要活动-换句话说,它在R.layout.activity_main搜索。 But it looks like you're looking for a View that's contained inside the Dialog View, autoReply . 但是看起来您正在寻找Dialog视图中包含的视图autoReply To get that, you'd need the second form. 为此,您需要第二种形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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