2013-08-01 104 views
0

有沒有辦法在非Activity類中使用openFileOutput()方法?有沒有什麼辦法可以在非Activity類中使用openFileOutput()方法?

我有私人類擴展AsyncTask,我想在onPostExecute()來執行的方法,但我不能這樣做,因爲我下面的錯誤IDE顯示:

的方法openFileOutput(字符串,整數)是未定義類型 FunkcjeAPI.Logowanie

什麼是替代品?我試圖使用getActivity().openFileOutput(),但後來我看到相同的錯誤。

有什麼建議嗎?

+0

THER沒有'openFileStream()'中的Android方法。 – CommonsWare

+0

是的,有:http://developer.android.com/reference/android/content/Context.html#openFileOutput%28java.lang.String,%20int%29 – TN888

+1

該方法未被命名爲'openFileStream()'。你可以通過閱讀來判斷。 – CommonsWare

回答

2

您的AsyncTask創建一個構造函數和傳遞的背景下,所以你以後可以使用它。

private class MyTask extends AsyncTask<Void, Void, Long> { 

    private Context context; 

    public MyTask (Context context) { 
     this.context = context; 
    } 

    // other code... 

    @Override 
    protected void onPostExecute(Long result) { 
     // use context here 
    } 
} 

然後,執行任務:

new MyTask(this).execute(); 
1

我解決我的問題,加入到我的AsyncTaskContext對象和打字的構造:

context.openFileOutput(); 
相關問題