2013-08-26 16 views
0

我在我的AsyncTask中有一個錯誤。我試圖用FileOutputStream保存數據到一個文件,因爲我需要這個數據永久。Android的AsyncTask使用openFileOutputStream

所以IAM閱讀本教程:Tutorial

但如果IAM將代碼添加到我的AsyncTask我得到這個錯誤:

「的方法openFileOutputStream(字符串,整數)是未定義的類型 MainActivity。 DownloadSpielplan」

DownloadSpielplan是我的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); 
     } 
    } 

我猜問題是,iam從AsyncTask調用openFileOutputStream,但我無法找到解決方案如何解決它。 (IAM原因Android中真的新)

+0

把你logcat錯誤。 – Hemant

+0

它不是一個logcat錯誤,它的錯誤eclipse告訴我,在這一行:FileOutputStream fos = openFileOutputStream(FILENAME,Context.MODE_PRIVATE); eclipse告訴我:「方法openFileOutputStream(String,int)對於MainActivity.DownloadSpielplan類型」 – user1662203

+0

「未定義,您必須調用openFileOutput,則必須使用活動上下文調用此方法。 http://goo.gl/UWhN1m – rajpara

回答

2

方法名稱是openFileOutput,不openFileOutputStream

呼叫

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

,而不是

openFileOutputStream(FILENAME, Context.MODE_PRIVATE) 

在doInBackground方法。

0

因爲您正在嘗試從非Activity類訪問方法openFileOutputStream。 相反,編寫代碼原樣

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

或其他選項會從活動調用其構造DownloadSpielplan是通過上下文和使用上下文打開文件輸出 -

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