简体   繁体   中英

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. I'm approaching this with 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. Does anyone have any idea how to fix this?

Edit:

Here's the 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. Otherwise, we can only guess which of the many possible references contained a null.

That said, I'm going to take a stab at this and guess, by elimination of other possibilities, that the culprit is autoreplytext . Although you didn't show us your XML for the views, I suspect that you need to change this line:

    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 . But it looks like you're looking for a View that's contained inside the Dialog View, autoReply . To get that, you'd need the second form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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