繁体   English   中英

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

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

我正在开发一个适用于Android的应用,该应用可以使某人向某人发送短信,并在他们发短信时自动回复该号码。 我正在使用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();
    }

有人更改默认消息的方法如下:

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;

问题是,每当我尝试使用AlertDialog更改默认消息时,都会出现NullPointerException错误。 有谁知道如何解决这个问题?

编辑:

这是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)

正如其他人在评论中所写,您确实不仅需要显示logcat的堆栈跟踪,还需要告诉我们第395行在哪里。 否则,我们只能猜测许多可能引用中的哪一个包含null。

就是说,我将对此采取行动,并通过消除其他可能性来猜测罪魁祸首是autoreplytext 尽管您没有向我们显示视图的XML,但我怀疑您需要更改以下行:

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

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

第一行在内容视图中搜索您的主要活动-换句话说,它在R.layout.activity_main搜索。 但是看起来您正在寻找Dialog视图中包含的视图autoReply 为此,您需要第二种形式。

暂无
暂无

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

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