16

最初に、メインアクティビティでいくつかのメソッドをコーディングしましたが、それらはクラスである必要があると判断しました。

これは私のコードです...openFileOutputとopenFileInputは未定義です。何か案が??多分それはサービスか活動であるべきです...??

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

}

4

2 に答える 2

35

これらはクラスで定義されたメソッドであり、Contextクラスで定義されたメソッドではありません。コードがActivityの一部である場合、その基本クラスで便利なメソッドopenFileInput()を使用して、基になるContext.getApplicationContext()。openFileInput()にアクセスできます(同様に)。ActivityopenFileOutput()

次に、それらを基礎となるContextメソッドへの直接呼び出しに置き換える必要があります。

于 2010-10-25T15:03:53.963 に答える
12

交換

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

下の行で

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

フラグメント内で使用する場合

FileOutputStream fos =getActivity().openFileOutput(filename, getActivity().MODE_PRIVATE);
于 2014-01-04T12:30:34.557 に答える