Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

java - No enclosing instance of the type is accessible in scope

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...