public void save(View v) {
switch(v.getId()) {
case R.id.bSave:
filename = "data/monday.txt";
vsName.showPrevious();
vsTime.showPrevious();
vsQuantity.showPrevious();
vsImage.showPrevious();
save.setVisibility(View.GONE);
try{
monday = new File(filename);
if (!monday.exists()) {
if (!monday.createNewFile()) {
throw new IOException("Unable to create file");
}
}
fos = new FileOutputStream(monday);
oos = new ObjectOutputStream(fos);
oos.writeObject(etName.getText().toString());
oos.writeObject(etQuantity.getText().toString());
oos.writeObject(etTime.getText().toString());
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally{
try {
fos.close();
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
}
这是我的save方法的代码。我希望它保存edittext,这样我的文本视图即使在关闭程序后也会保留。这是我第一次使用fileoutputstreams所以我不知道如何解决这个问题。当我点击保存按钮时,不幸的是它已经停止了。
05-22 15:32:40.095: E/AndroidRuntime(6984): FATAL EXCEPTION: main
05-22 15:32:40.095: E/AndroidRuntime(6984): java.lang.IllegalStateException: Could not execute method of the activity
05-22 15:32:40.095: E/AndroidRuntime(6984): at android.view.View$1.onClick(View.java:3599)
05-22 15:32:40.095: E/AndroidRuntime(6984): at android.view.View.performClick(View.java:4204)
05-22 15:32:40.095: E/AndroidRuntime(6984): at android.view.View$PerformClick.run(View.java:17355)
05-22 15:32:40.095: E/AndroidRuntime(6984): at android.os.Handler.handleCallback(Handler.java:725)
05-22 15:32:40.095: E/AndroidRuntime(6984): at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 15:32:40.095: E/AndroidRuntime(6984): at android.os.Looper.loop(Looper.java:137)
05-22 15:32:40.095: E/AndroidRuntime(6984): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-22 15:32:40.095: E/AndroidRuntime(6984): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 15:32:40.095: E/AndroidRuntime(6984): at java.lang.reflect.Method.invoke(Method.java:511)
05-22 15:32:40.095: E/AndroidRuntime(6984): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-22 15:32:40.095: E/AndroidRuntime(6984): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-22 15:32:40.095: E/AndroidRuntime(6984): at dalvik.system.NativeStart.main(Native Method)
05-22 15:32:40.095: E/AndroidRuntime(6984): Caused by: java.lang.reflect.InvocationTargetException
05-22 15:32:40.095: E/AndroidRuntime(6984): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 15:32:40.095: E/AndroidRuntime(6984): at java.lang.reflect.Method.invoke(Method.java:511)
05-22 15:32:40.095: E/AndroidRuntime(6984): at android.view.View$1.onClick(View.java:3594)
05-22 15:32:40.095: E/AndroidRuntime(6984): ... 11 more
05-22 15:32:40.095: E/AndroidRuntime(6984): Caused by: java.lang.NullPointerException
05-22 15:32:40.095: E/AndroidRuntime(6984): at tbjsoft.medicationreminder.Monday.save(Monday.java:170)
05-22 15:32:40.095: E/AndroidRuntime(6984): ... 14 more
答案 0 :(得分:0)
您最好使用共享首选项将文本保存在edittext中。
How to use SharedPreferences in Android to store, fetch and edit values
除非您放置使用此方法的代码,否则此处显示的方法无助于解决问题。