tags:

views:

252

answers:

2

Im trying to use openFileOutput function but it doesnt want to compile... doesnt recognize de function. Im using android sdk 1.6. Is this a sdk problem ? Is this a parameter problem ?

import java.io.FileOutputStream;
public static void save(String filename, MyObjectClassArray[] theObjectAr) {
        FileOutputStream fos;
        try {
            fos = openFileOutput(filename, Context.MODE_PRIVATE);


            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(theObjectAr); 
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
A: 

You're trying invoke non-static method from static context (your method has static modifier). You either have to make your method to be non-static or to pass in an instance of Context (activity instance in most cases) and invoke the method on the object.

Konstantin Burov
Thanks I found out also this problem. :)
Fabien
A: 

Your method should be as follows. Takes in an extra Context as a parameter. To this method you can pass your Service or Activity

public static void save(String filename, MyObjectClassArray[] theObjectAr, 
  Context ctx) {
        FileOutputStream fos;
        try {
            fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);


            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(theObjectAr); 
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
naikus
thanks, this is it.
Fabien