Ah!!! So you have a *compile* error!

Ok, try this:

        String fil = "test2";
        String string = "exempeltext";

        FileOutputStream fos;

        try {

            fos = openFileOutput(fil, Context.MODE_PRIVATE);

            fos.write(string.getBytes());
            fos.close();
        } catch (IOException x) {
               Log.i(TAG, "Cannot open file: " + x.toString());
        }

Eclipse (or any other Java compiler) checks exception signatures at compile time.

The compiler knows that openFileOutput *can* throw a FileNotFoundException, since it's declared like this:

public FileOutputStream openFileOutput(String name, int mode)
*throws FileNotFoundException*

It also knows that write() and close() *can* throw IOException.

Since onCreate says it does not throw any exceptions, the compiler flags this as an error, and suggests that you either:

1) catch any exceptions from the above methods, and don't let them propagate out of onCreate or

2) change the signature of onCreate to indicate that it, too, can throw exceptions (which is not really an option, because onCreate is an override of a method of Activity, and you can't add new exceptions when overriding methods).

Since exception handling is one of the fundamentals of Java, you should take some time to learn more about them. There are lots of articles and samples on the net, here is one (from 1998, and things haven't changed since then):

http://www.javaworld.com/jw-07-1998/jw-07-exceptions.html

-- Kostya

28.01.2011 16:58, Michael пишет:
Thanks for the answer and for testing the code. I'm developing on
Eclipse - this is my first try on android developing and my java isn't
that good in the first place. I'm using a virtual andriod phone for
testing, but i can't even compile the code because of the error
message. I will try and reinstall Eclipe or another API and see if it
helps.

On 28 Jan, 14:40, Kostya Vasilyev<kmans...@gmail.com>  wrote:
Michael,

Your code (as posted) is correct. Just in verify, I just pasted in
verbatim into my application and verified that it works (as it should).

Try removing and reinstalling the application, see if that helps.

Other than that - is this a Samsung Galaxy S with 2.2.1 firmware, by any
chance?

-- Kostya

28.01.2011 15:45, Michael пишет:

On 27 Jan, 19:52, TreKing<treking...@gmail.com>    wrote:
You do not need external storage permission to save local data.
Any other permissions than can be the issue? I've tried reading some
documentation for the answer, but i can't find anything about internal
storage and permission.
--
Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to