12

I have this code:

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:

No enclosing Instance of the type SoftwarePassionView is accessible in scope

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Christian
  • 958
  • 5
  • 23
  • 52

3 Answers3

11

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".

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.


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);
        }
    }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I honesty am not sure what you are saying. Would it be possible to get an example. and this is a non-static inner. – Christian Mar 26 '11 at 00:50
6

You are trying to get this for the enclosing class, which would work if you were inside an anonymous class, but I'm guessing this is not the case.

Please read http://mindprod.com/jgloss/this.html

Lucky
  • 16,787
  • 19
  • 117
  • 151
stevebot
  • 23,275
  • 29
  • 119
  • 181
4

If you want to put your snippet inside a different class rather than SoftwarePassionView, you can pass an instance of the class SoftwarePassionView in your thread constructor.

Here is an example:

Class SoftwarePassionView {
  ....

  Thread thread = new something(SoftwarePassionView);
  thread.start();
  ......
  }

In the other class

 Class something extends Thread{
  SoftwarePassionView SPV;
  something(SoftwarePassionView){
  super(null, vieworders, "MagentoBackground");
  this.SPV = SoftwarePassionView}
  }

   @Override
public void run(){
    m_progressDialog = ProgressDialog.show(SPV, 
    "Please wait...", "Retrieving data...", true);
     }

  }
yasserbn
  • 391
  • 3
  • 18