在引用内部类的Activity时Kotlin错误

我有一个AsyncTask作为我在Kotlin写的Activity中的一个内部类。 现在,我试图从我的AsyncTask的onPostExecute使用this@MyActivity访问Activity,但Android Studio报告它为Unresolved Reference错误。 但是这是在线建议的从InnerClass引用OuterClass最常用的方法。 代码如下:

 class MyActivity : AbstractAppPauseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } override fun onResume() { super.onResume() } class MyTask(private var mContext: Context?, val pbMigrating: ProgressBar) :AsyncTask<Void, Int, Void>() { private var size: Long = 0 override fun onPreExecute() { super.onPreExecute() ... } override fun doInBackground(vararg params: Void?): Void? { ... return null } override fun onProgressUpdate(vararg values: Int?) { super.onProgressUpdate(*values) pbMigrating.progress = values[0] as Int } override fun onPostExecute(result: Void?) { super.onPostExecute(result) this@MyActivity //<- Cannot Resolve error here } } } 

你必须把课堂作为内心

 class MyActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } override fun onResume() { super.onResume() } inner class MyTask(private var mContext: Context?, val pbMigrating: ProgressBar) : AsyncTask<Void, Int, Void>() { private var size: Long = 0 override fun onPreExecute() { super.onPreExecute() } override fun doInBackground(vararg params: Void?): Void? { return null } override fun onProgressUpdate(vararg values: Int?) { super.onProgressUpdate(*values) pbMigrating.progress = values[0] as Int } override fun onPostExecute(result: Void?) { super.onPostExecute(result) this@MyActivity //<- Cannot Resolve error here } } 

}

MyTask类必须被定义为一个内部类:

 inner class MyTask(private var mContext: Context?, val pbMigrating: ProgressBar) :AsyncTask<Void, Int, Void>() { ... }