0

我正在为 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 错误。有谁知道如何解决这个问题?

编辑:

这是日志猫:

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)
4

1 回答 1

0

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

也就是说,我将对此进行尝试,并通过消除其他可能性来猜测罪魁祸首是autoreplytext. 尽管您没有向我们展示您的 XML 视图,但我怀疑您需要更改这一行:

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

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

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

于 2013-09-09T21:38:17.220 回答