简体   繁体   English

Java Android不是封闭类错误

[英]Java Android Not an enclosing class error

I'm new in Java and Android, i have to use a method inside another but it doesn't work. 我是Java和Android的新手,我必须在另一个方法中使用一种方法,但是它不起作用。 when i setContentView to new SurfaceViewStart.OurView(this) in the DrawingCanvass Java class it throw an error message " SurfaceViewStart is not an enclosing class ". 当我在DrawingCanvass Java类中将ContentView设置为新的SurfaceViewStart.OurView(this)时,抛出错误消息“ SurfaceViewStart不是封闭类 ”。 Thanks in anticipation 谢谢你的期待

This is the SurfaceViewStart Java class 这是SurfaceViewStart Java类

public class SurfaceViewStart extends Activity implements View.OnTouchListener {

OurView v;
Bitmap ball;
float x, y;
@Override
protected void onCreate (Bundle saveInstanceState){
    super.onCreate(saveInstanceState);
    v = new OurView(this);
    //the v variable is calling the context of the constructor under public class OurView
    v.setOnTouchListener(this);
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.wall7);
    x = 0;
    y = 0;
    setContentView(v);
}

@Override
protected void onPause(){
    super.onPause();
}

@Override
protected void onResume(){
    super.onResume();
}


public class OurView extends SurfaceView implements Runnable {
    //implementing Runnable makes the object of the class to act as a thread
    //this class will handle the drawing in the canvass
    //public class Ourview is a constructor

    Thread tie = null;
    SurfaceHolder holder;
    //this allow us to change the dimension of our view
    Boolean value = false;

    public OurView (Context context){
        super(context);

        holder = getHolder();

    }

    public void run(){
    //the public void run() is introduce after implementing Runnable
    //the surfaceview becomes a thread
    //this run the thread

        while (value == true){
            //this performs the drawing on the canvass
            //it is called invalidate, which causes the thread to loop through
            if (!holder.getSurface().isValid()){
                //if holder.getSurface is not valid, i.e our surface is not available

                continue;
                //it will go back and loop again checking if value == true and also if the surface is available, continue to
                //loop if it's not available
            }
            //our canvass will then be created here

            Canvas can = holder.lockCanvas();
            //the canvass is normally lock before drawing and  unlock it on display

            can.drawARGB(255, 150, 120, 100);
            //the A stands for Alpha and this will paint our canvass

            can.drawBitmap(ball, x - (can.getWidth()/2), y - (can.getHeight()/2), null);

            holder.unlockCanvasAndPost(can);
            //this will unlock the canvass and display the drawing
            //no Bitmap is drawn now untill we add an onCreate method to the Mainclass SurfaceViewStart

        }

    }

    public void pause(){
        //this pause the thread

        value = false;

        while (true){
            try {
                tie.join();
                //these blocks the current thread until  the receiver finishes the execution and die
            }catch (InterruptedException d){
                d.printStackTrace();
            }
            break;
        }

        tie = null;
    }

    public void resume(){
        //this resume the thread

        value = true;
        tie = new Thread(this);
        // 'this' used in the new thread refer to the run class which has been implemented
        tie.start();

    }
}
public boolean onTouch(View v, MotionEvent we){
    //this handles
    return false;
}

}

This is the DrawingCanvas Java Class 这是DrawingCanvas Java类

public class DrawingCanvass extends Activity {

SurfaceViewStart.OurView v;
//SurfaceViewStart v;

@Override
protected void onCreate (Bundle saveInstanceState){
    super.onCreate(saveInstanceState);

    v = new SurfaceViewStart.OurView(this);
    setContentView(v);
}
}

to fully qualify your inner class, using the outer class name, your inner class has to be static. 要完全限定您的内部类,请使用外部类名称,您的内部类必须是静态的。

public static class OurView extends SurfaceView implements Runnable {

otherwise you need an instance of the outer class to access it. 否则,您需要外部类的实例来访问它。 Like 喜欢

 outerClassInstace.new OurView(this);

which is not the case for DrawingCanvass , since you don't an instance of the other Activity 这不是DrawingCanvass的情况,因为您没有另一个Activity的实例

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

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