2014-10-19 65 views
0

我在一個分離的類中使用全局變量「GlobalVariables」,我嘗試在下面的代碼中使用它,但它總是給我錯誤:該方法getApplication()是未定義的類型(我的課程)

The method getApplication() is undefined for the type UploadPicture 

我嘗試以下,但仍然有錯誤:

((GlobalVariables) this.getApplication()).set_FileUploading(false); 

的qustion已經要求在這裏,但unfortunatlly所有answors沒有和我一起工作,給了我同樣的錯誤!有什麼建議嗎?

public class UploadPicture extends AsyncTask<Void, Long, Boolean> { 

private DropboxAPI<?> mApi; 
private String mPath; 
private File mFile; 
private long mFileLen; 
private UploadRequest mRequest; 
private Context mContext; 
private String mErrorMsg; 
private File outFiles; 

public UploadPicture(Context context, DropboxAPI<?> api, String dropboxPath, File file) { 
    mContext = context.getApplicationContext(); 
    mFileLen = file.length(); 
    mApi = api; 
    mPath = dropboxPath; 
    mFile = file;   
} 
@Override 
protected Boolean doInBackground(Void... params) { 
    try { 
     FileInputStream fis = new FileInputStream(mFile); 
     String path = mPath + outFiles.getName(); 
     mRequest = mApi.putFileOverwriteRequest(path, fis, mFile.length(), 
       new ProgressListener() { 
      @Override 
      public long progressInterval() { 
       return 500; 
      } 

      @Override 
      public void onProgress(long bytes, long total) { 
       //publishProgress(bytes); 
      } 
     } 
     ); 

     if (mRequest != null) { 
      mRequest.upload(); 
      ((GlobalVariables) UploadPicture.this.getApplication()).set_FileUploading(false);    
      return true; 
     } 

    } catch (DropboxUnlinkedException e) { 
     // This session wasn't authenticated properly or user unlinked 
     mErrorMsg = "This app wasn't authenticated properly."; 
    } catch (DropboxFileSizeException e) { 
     // File size too big to upload via the API 
     mErrorMsg = "This file is too big to upload"; 
    } catch (DropboxPartialFileException e) { 
     // We canceled the operation 
     mErrorMsg = "Upload canceled"; 
    } catch (DropboxServerException e) { 
     // Server-side exception. These are examples of what could happen, 
     // but we don't do anything special with them here. 
     if (e.error == DropboxServerException._401_UNAUTHORIZED) { 
      // Unauthorized, so we should unlink them. You may want to 
      // automatically log the user out in this case. 
     } else if (e.error == DropboxServerException._403_FORBIDDEN) { 
      // Not allowed to access this 
     } else if (e.error == DropboxServerException._404_NOT_FOUND) { 
      // path not found (or if it was the thumbnail, can't be 
      // thumbnailed) 
     } else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) { 
      // user is over quota 
     } else { 
      // Something else 
     } 
     // This gets the Dropbox error, translated into the user's language 
     mErrorMsg = e.body.userError; 
     if (mErrorMsg == null) { 
      mErrorMsg = e.body.error; 
     } 
    } catch (DropboxIOException e) { 
     // Happens all the time, probably want to retry automatically. 
     mErrorMsg = "Network error. Try again."; 
    } catch (DropboxParseException e) { 
     // Probably due to Dropbox server restarting, should retry 
     mErrorMsg = "Dropbox error. Try again."; 
    } catch (DropboxException e) { 
     // Unknown error 
     mErrorMsg = "Unknown error. Try again."; 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    return false; 
} 

}

編輯:我現在將我的 「VariableGlobales」 CALSS:

public class GlobalVariables extends Application { 
private Boolean _IsIOIORunning=false; 
private Boolean _FileUploading=false; 

public Boolean get_IsIOIORunning() 
{ 
    return _IsIOIORunning; 
}  
public void set_IsIOIORunning(Boolean _IsIOIORunning) 
{ 
    this._IsIOIORunning = _IsIOIORunning; 
} 


public Boolean get_FileUploading() 
{ 
    return _FileUploading; 
}  
public void set_FileUploading(Boolean _FileUploading) 
{ 
    this._FileUploading = _FileUploading; 
} 
+0

我什麼的getApplication方法呢?爲什麼在GlobalVariables類中保留該引用? – santalu 2014-10-19 12:01:53

+0

什麼是GlobalVariables?它是否擴展應用程序? – ToYonos 2014-10-19 12:09:50

回答

0

這是正常的UploadPicture不延長GlobalVariables但延長的AsyncTask。

,它是我的 「GlobalVariables」

public class AppInfo extends Application { 

    private static Context context; 
    private static String user; 

    public void onCreate(){ 
     super.onCreate(); 
     AppInfo.context = getApplicationContext(); 
     user = null; 
    } 

    public static Context getAppContext()   {return AppInfo.context;} 
    public static String getUser()     {return user;} 

    public static void setUser(String user)   {AppInfo.user = user;} 
} 

我到處叫它像:

AppInfo.getUser(); 

編輯:

GlobalVariables應該使用靜態方法和變量:

public class GlobalVariables extends Application { 
private static Boolean _IsIOIORunning=false; 
private static Boolean _FileUploading=false; 

public static Boolean get_IsIOIORunning() { 
    return _IsIOIORunning; 
} 

public static void set_IsIOIORunning(Boolean _IsIOIORunning) { 
    GlobalVariables._IsIOIORunning = _IsIOIORunning; 
} 

public static Boolean get_FileUploading(){ 
    return _FileUploading; 
} 

public static void set_FileUploading(Boolean _FileUploading){ 
    GlobalVariables._FileUploading = _FileUploading; 
} 
} 
+0

我實際上已經導入了GlobaleVariables calss「import my.Application.Name.GlobalVariables;」但仍然無法正常工作。所以,你的意思是我們不能在這裏使用全局變量? – 2014-10-19 12:05:11

+0

因此,應該工作:GlobalVariables.this.getApplication()。set_FileUploading(false);.我是,在我的android應用程序中使用一個全局類,所以它應該與你一起工作 – Marchah 2014-10-19 12:06:23

+0

我得到了:「GlobalVariables類型沒有封閉實例可以在範圍內訪問」,也沒有定義類型GlobalVariables的getApplication()方法「 – 2014-10-19 12:09:33

相關問題