0

我的 AsyncTask 有错误。我试图用 FileOutputStream 将数据保存到文件中,因为我需要这个数据永久。

所以我正在阅读本教程:教程

但是,如果我将代码添加到我的 AsyncTask 中,我会收到此错误:

“方法 openFileOutputStream(String, int) 未定义 MainActivity.DownloadSpielplan 类型”

下载Spielplan 是我的 AsyncTask

private class DownloadSpielplan extends AsyncTask <Void, Void, String>
    {
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }
        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub


            //dont wonder reverseString is created and filled i delete this part from the code for more readability


            FILENAME = "SpielTag";
            JOUR = reverseString;

            FileOutputStream fos = openFileOutputStream(FILENAME, Context.MODE_PRIVATE);
            fos.write(JOUR.getBytes());
            fos.close();


            return reverseString;
        }

        @Override
        protected void onPostExecute(String reverseString) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Download abgeschlossen!", Toast.LENGTH_LONG).show();
            super.onPostExecute(reverseString);
        }
    }   

我想问题是,我正在从 AsyncTask 调用 openFileOutputStream,但我找不到如何解决它的解决方案。(因为我在 Android 中真的很新)

4

2 回答 2

2

方法名称是 openFileOutput,而不是 openFileOutputStream

称呼

MainActivity.this.openFileOutput(FILENAME, Context.MODE_PRIVATE)

代替

openFileOutputStream(FILENAME, Context.MODE_PRIVATE)

在您的 doInBackground 方法中。

于 2013-08-26T12:56:24.070 回答
0

因为您试图openFileOutputStream从一个不是Activity. 相反,将您的代码编写为-

FileOutputStream fos = MainActivity.this.openFileOutputStream(FILENAME, Context.MODE_PRIVATE);

或者其他选项是从在其构造函数中调用的活动传递上下文DownloadSpielplan并使用上下文打开文件输出 -

FileOutputStream fos = context.openFileOutputStream(FILENAME, Context.MODE_PRIVATE);
于 2013-08-26T13:03:46.737 回答