Domanda

In primo luogo, ho codificato alcuni metodi in Main Activity, ma ho deciso che dovrebbe essere una classe.

Questo è il mio codice ... openFileOutput e openFileInput sono indefiniti. Qualche idea?? forse dovrebbe essere il servizio o attività ... ??

    package spexco.hus.system;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Date;
    import spexco.hus.cepvizyon.CepVizyon;
    import android.content.Context;

    public class LicenseIDB {
    private String PHONECODEFILE = "CepVizyonCode";
    private static String PhoneCode = null;

    public LicenseIDB() {
    if (readLocal(PHONECODEFILE, 8) == null)
        createSystemCode();
}

public static long getDate() {
    Date currentTime = new Date();
    return currentTime.getTime();
}

public void createSystemCode() {
    long date = getDate();
    String str = Integer.toHexString(Integer.MAX_VALUE - (int) date);
    for (int i = str.length(); i < 8; i++) {
        str += "" + i;
    }
    PhoneCode = str.substring(0, 8);
    saveLocal(PhoneCode, PHONECODEFILE);

}

public static String getPhoneCode() {

    return PhoneCode;
}

public void saveLocal(String fileString, String Adress) {

    try {
        FileOutputStream fos = openFileOutput(Adress, Context.MODE_PRIVATE);
        fos.write(fileString.getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public String readLocal(String Adress, int lenght) {
    byte[] buffer = new byte[lenght];
    String str = new String();
    try {
        FileInputStream fis = openFileInput(Adress);
        fis.read(buffer);
        fis.close();
        str = new String(buffer);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return str;
}

}

È stato utile?

Soluzione

Questi sono metodi definiti sulla classe Context, non i metodi definiti nella classe. Quando il codice faceva parte di un Attività , si potrebbe utilizzare un metodo comodo openFileInput () nella sua classe base Activity accedere sottostante Context.getApplicationContext (). openFileInput () (e analogamente per openFileOutput()).

Ora si dovrà sostituire quelli con le chiamate dirette ai sottostanti metodi contesto .

Altri suggerimenti

Sostituire

FileOutputStream fos = openFileOutput(Adress, Context.MODE_PRIVATE);

con sotto la linea

FileOutputStream fos = getApplicationContext().openFileOutput(filename, getActivity().MODE_PRIVATE);

Se utilizzato all'interno Frammento

FileOutputStream fos =getActivity().openFileOutput(filename, getActivity().MODE_PRIVATE);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top