我是Java和Android的新手,我必须在另一个内部使用一种方法,但它不起作用。当我在DrawingCanvass Java类中将setContentView设置为新的SurfaceViewStart.OurView(this)时,它会抛出一条错误消息" SurfaceViewStart不是一个封闭的类"。 在期待中感谢
这是 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;
}
}
这是 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);
}
}
答案 0 :(得分:2)
要使用外部类名完全限定内部类,内部类必须是静态的。
public static class OurView extends SurfaceView implements Runnable {
否则您需要外部类的实例来访问它。像
outerClassInstace.new OurView(this);
不是DrawingCanvass
的情况,因为您没有其他活动的实例