范围内不能访问该类型的封闭实例 [英] No enclosing instance of the type is accessible in scope
问题描述
我有这段代码:
Thread thread = new Thread(null, vieworders, "MagentoBackground");
thread.start();
m_progressDialog = ProgressDialog.show(SoftwarePassionView.this,
"Please wait...", "Retrieving data...", true);
这会产生以下编译错误:
This gives the following compilation error:
在范围内无法访问类型为SoftwarePassionView的封闭实例
No enclosing Instance of the type SoftwarePassionView is accessible in scope
这是如何引起的,我该怎么办?解决它?
How is this caused and how can I solve it?
推荐答案
表达式 SoftwarePassionView.this
只有在示例代码段中的代码位于非静态内部/嵌套类中,其中一个封闭类是 SoftwarePassionView
。它说给我封闭的 SoftwarePassionView
实例。
The expression SoftwarePassionView.this
only makes sense if the code in the example snippet is found in a non-static inner / nested class, and one of the enclosing classes is SoftwarePassionView
. It says "give me the enclosing SoftwarePassionView
instance".
如果此代码是不是在那个上下文中,(因为编译器错误会指示),你需要用普通的变量名替换表达式,或者用方法调用来引用一些 SoftwarePassionView
对象。
If this code is not in that context, (as the compiler error would indicate), you need to replace the expression with a normal variable name, or method call that gives a reference to some SoftwarePassionView
object.
对于记录,这是一个例子,其中 SoftwarePassionView.this
不会出现编译错误:
For the record, here is an example where SoftwarePassionView.this
wouldn't be a compilation error:
public class SoftwarePassionView {
public class Inner {
...
public void doIt() {
Thread thread = new Thread(null, vieworders, "MagentoBackground");
thread.start();
m_progressDialog = ProgressDialog.show(SoftwarePassionView.this,
"Please wait...", "Retrieving data...", true);
}
}
}
这篇关于范围内不能访问该类型的封闭实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!