android-asynctask - 在 AsyncTask 的 getApplication() 上找不到符号

标签 android-asynctask android

我正在使用 AsyncTask 并希望使用 getApplication() 来处理类 Application。 但是在 getApplication() 上出错找不到符号。

我的代码:

public class  splash extends AsyncTask {
  protected config app;
  public  splash(Context context) {
  super();
  app = ((config) getApplication());
  this.context=context;
}

以及我想使用的类:

public class config extends Application
{
  public Boolean  con=true;
  public int status=-1;
  public String actNum; 
  public void onCreate()
  {
    super.onCreate();
  }
}

最佳答案

如果你想获得 Application 实例,你可以在 onCreate() 中用它初始化一个成员,并通过类方法返回它:

public class ApplicationConfig extends Application {

  private static ApplicationConfig instance;

  public void onCreate() {
    super.onCreate();
    instance = this;
  }

  public static ApplicationConfig getConfig() {
    return instance;
  }
}

然后您可以通过以下方式在任何地方检索此实例:

ApplicationConfig conf = ApplicationConfig.getConfig();

关于android-asynctask - 在 AsyncTask 的 getApplication() 上找不到符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11167774/

相关文章:

android - 如何正确管理Android后台任务?

java - Android:如何在主要 Activity 的变量中存储值(value)?

android - 从 AsyncTask 或服务通知 UI 组件的最佳实践

android - cwac-相机 : SingleShotMode: picture taken not being showed up on S3 and S4

android - AsyncTask 的行为

android - 尽管使用弱引用内存泄漏

android - 如何在 adb shell 中获得 root 访问权限以解锁模式

android - 从另一个类调用 mainActivity 中的函数

android - 如何在 Kotlin 中获取 Mac 地址

java - TextWatcher 中的编辑文本问题