AsyncTask类中的非法前向引用扩展BaseActivity

时间:2019-02-18 03:41:27

标签: java android android-asynctask

当日食项目迁移到Android Studio时,我遇到此错误。我在3个班级遇到此错误。所有这些类都扩展了 BaseActivity 并从 BaseActivity 调用字段。例如,在onPostExcuteMethod内的类 SavingActivity 中,我从 BaseActivity 调用名为mProgressDlg的字段,并且IDE指向该字段的非法前向引用(嗯,在此下方没有红线显示)。

BaseActivity 类代码的一部分

public abstract class BaseActivity extends Activity implements OnClickListener {

public Button mBtnRefresh = null;
public Context mContext = null;
public GridView mGridView = null;
public ImageLoader mImageLoader = null;
public static ProgressDialog mProgressDlg;
public boolean mRefreshing = false;
public int mScreenWidth = 0;
public TextView mTxtNoData = null;
SavingActivity 中的

onPostExcute 方法:

protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (SavingActivity.this.mProgressDlg != null) {
            try {
                SavingActivity.this.mProgressDlg.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        SavingActivity.this.mProgressDlg = null;
        if (SavingActivity.this.mMusicInfo != null) {
            SavingActivity.this.mStrMusicFilePath = SavingActivity.this.mContext.getFilesDir() + CookieSpec.PATH_DELIM + SavingActivity.this.mMusicInfo.strMusicFileName;
            SavingActivity.this.refreshPage();
        }
    }
}

让我知道您是否还需要其他分析。

编辑:我什至注释掉了onPostExecute方法,并尝试从cli gradlew lintRelease构建apk,但它指出了错误“非法向前引用我实际上所在的行”评论。请查看所附图片。

cli build apk error

commented onPostExecute method

来自终端的错误日志:

> Task :app:compileReleaseJavaWithJavac FAILED
E:\Android-Projects-Workspace\name\app\src\main\java\com\name\activities\SavingActivity.java:113: error: illegal forward reference
            if (BaseActivity.mProgressDlg != null) {
             ^

2 个答案:

答案 0 :(得分:1)

要解决“ 非法前向引用”,您必须知道在Java中不允许在下面两行读取静态字段mProgressDlg,请参见static Fields constrains

SavingActivity.this.mProgressDlg != null

SavingActivity.this.mProgressDlg.dismiss()

解决方案是使mProgressDlgBaseActivity完全符合条件,即

BaseActivity.mProgressDlg

答案 1 :(得分:0)

Android Studio似乎将您指向该错误String.translate的错误类。好吧,实际上编译器会在包内的任何一个类中遇到该错误。因此,请检查所有类,您肯定会找到具有illegal forward reference的实际类。