2013-08-26 20 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);