简体   繁体   English

在MainActivity.this范围内无法访问MainActivity类型的封闭实例。

[英]No enclosing instance of the type MainActivity is accessible in scope at MainActivity.this

I am making an app with a ListView. 我正在使用ListView制作应用程序。 That ListView needs a separate class where you can set the action that happens when you touch an item in the ListView. 该ListView需要一个单独的类,您可以在其中设置在触摸ListView中的项目时发生的操作。 I programmed it do download a file and show a ProgressDialog. 我对它进行了编程,可以下载文件并显示ProgressDialog。

The error is at this line of code: 错误在以下代码行:

myDialog = new ProgressDialog(MainActivity.this);

At MainActivity.this. 在MainActivity.this。

Could someone help me to fix the problem? 有人可以帮我解决问题吗?

The whole class is at http://pastebin.com/3rWEKz4r 整个课程在http://pastebin.com/3rWEKz4r

The problem is that you are trying to reference MainActivity from a different class that reside in completely different files. 问题是您要尝试从驻留在完全不同的文件中的另一个类引用MainActivity

Also, it is not the BaseAdapter 's job to inflate and display a Dialog ; 另外,膨胀和显示Dialog不是BaseAdapter的工作; it is better design to have the Activity do it. 最好由Activity来设计。 Doing this will also solve the problem you are having since you will be able to reference the Activity directly. 这样做还可以解决您遇到的问题,因为您将能够直接引用该Activity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM