-1

我已将一些代码从普通方法移至 AsyncTask doInBackground 方法,除了文件读取器外,一切正常。我得到“无法解决上下文”。我已经尝试完全删除“context.getApplicationContext()”,但这只是让我“对于 AlarmReceiver.backgroundputFTP 类型的方法 openFileInput(String) 未定义”。请问有人有什么想法吗?

public void putFTP(Context context)
     {
         new backgroundputFTP().execute();  
     }

  private class backgroundputFTP extends AsyncTask< Void, Void,Void >
  {  

        @Override  
        protected Void doInBackground(Void... params) 
        {  
     //
 // Push query result text file to FTP server
 //

  FTPClient client = new FTPClient();
  FileInputStream fis = null;
  Looper.prepare(); 

  try {
      client.connect(ipAddress);
      client.enterLocalPassiveMode();
      client.login(user, pass);

      //
      // Create an InputStream of the file to be uploaded
      //
      filename = "sdcardstats.txt";
      fis = context.getApplicationContext().openFileInput(filename);
4

2 回答 2

0
 fis = context.getApplicationContext().openFileInput(filename);

你在哪里定义了这个上下文?尝试这个 。

public Context cContext ;
public void putFTP(Context context)
     {
           cContext = context;
         new backgroundputFTP().execute();  
     }

  private class backgroundputFTP extends AsyncTask< Void, Void,Void >
  {  

        @Override  
        protected Void doInBackground(Void... params) 
        {  
     //
 // Push query result text file to FTP server
 //

  FTPClient client = new FTPClient();
  FileInputStream fis = null;
  Looper.prepare(); 

  try {
      client.connect(ipAddress);
      client.enterLocalPassiveMode();
      client.login(user, pass);

      //
      // Create an InputStream of the file to be uploaded
      //
      filename = "sdcardstats.txt";
      fis = cContext.openFileInput(filename);
于 2012-09-05T18:00:31.360 回答
0

尝试这个:

fis = YourActivity.this.getContext().openFileInput(filename);

YourActivityActivity您拥有 AsyncTask 的地方

于 2012-09-05T16:16:44.070 回答