我正在为 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)