0
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;
    }
}

That's the code for my save method. I want it to save the edittext so my textviews will stay even after i close the program. This is my first time using the fileoutputstreams so i don't know how to fix the problem. When i click the save button, it says unfortunately it has stopped.

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
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
ThatBoiJo
  • 319
  • 2
  • 14

1 Answers1

0

you better use shared preferences to save the text in edittext.

How to use SharedPreferences in Android to store, fetch and edit values

the method you show here is no help to solve the problem unless you place the code in which you use this method.

Community
  • 1
  • 1
Narasimha
  • 759
  • 6
  • 8
  • Yeah, i've switched it since then. Now i'm having a problem when I hit the back button, or close the app. The values for the textviews don't stay. – ThatBoiJo May 22 '13 at 23:15
  • I actually just figured out how to, but do you know if i can use sharedpreferneces to save images? – ThatBoiJo May 22 '13 at 23:33
  • "prefs.edit().put" when you type this you will get some suggestions from android. For now only those are the available options to save preferences. You better cache the images. – Narasimha May 23 '13 at 00:05