Search code examples
javaandroidsdkprogressdialogmain-activity

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


I am making an app with a ListView. That ListView needs a separate class where you can set the action that happens when you touch an item in the ListView. I programmed it do download a file and show a ProgressDialog.

The error is at this line of code:

myDialog = new ProgressDialog(MainActivity.this);

At MainActivity.this.

Could someone help me to fix the problem?

The whole class is at http://pastebin.com/3rWEKz4r


Solution

  • The problem is that you are trying to reference MainActivity from a different class that reside in completely different files.

    Also, it is not the BaseAdapter's job to inflate and display a Dialog; it is better design to have the Activity do it. Doing this will also solve the problem you are having since you will be able to reference the Activity directly.