0

I'm new in Java and Android, i have to use a method inside another but it doesn't work. when i setContentView to new SurfaceViewStart.OurView(this) in the DrawingCanvass Java class it throw an error message "SurfaceViewStart is not an enclosing class". Thanks in anticipation

This is the SurfaceViewStart Java class

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

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);
}
}
Saveen
4,23014 gold badges42 silver badges43 bronze badges
asked Sep 4, 2016 at 9:09

1 Answer 1

3

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

answered Sep 4, 2016 at 9:14
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help @Blackbelt, but I now notice a new error in the static class "a non static field 'ball, x, y' cannot be reference from a static context"

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.