它一直说构造函数是未定义的。我已经读过一些我需要声明没有参数的构造函数的地方。我只是不知道该怎么做。
如果有人可以提供帮助,我是java和编程的新手。我的代码在
之下package com.alextrost.onscreenjoystickdemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameSurface1 extends SurfaceView implements SurfaceHolder.Callback {
private Context _context1;
private GameThread1 _thread1;
private GameControls1 _controls1;
private GameJoystick1 _joystick1;
private Bitmap _pointer;
public GameSurface1(Context context) {
super(context);
// TODO Auto-generated constructor stub
_context1 = context;
init1();
}
private void init1(){
SurfaceHolder holder = getHolder();
holder.addCallback( this);
// A call will be made to start it later
_thread1 = new GameThread1(holder, _context1, new Handler(),this);
setFocusable(true);
_joystick1 = new GameJoystick1(getContext().getResources());
_pointer = (Bitmap)BitmapFactory.decodeResource(getResources(), R.drawable.icon);
//controls
_controls1 = new GameControls1();
setOnTouchListener(_controls1);
}
public void doDraw1(Canvas canvas){
//update the pointer
_controls1.update(null);
//draw the pointer
canvas.drawBitmap(_pointer, _controls1._pointerPosition1.x, _controls1._pointerPosition1.y, null);
//draw the joystick background
canvas.drawBitmap(_joystick1.get_joystickBg(), 50,220, null);
//draw the dragable joystick
canvas.drawBitmap(_joystick1.get_joystick(),_controls1._touchingPoint1.x -357, _controls1._touchingPoint1.y -26, null);
}
//these methods are overridden from the SurfaceView super class. They are automatically called
//when a SurfaceView is created, resumed or suspended.
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}
private boolean retry1;
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
retry1 = true;
//code to end gameloop
_thread1.state1 = GameThread.STOPED;
while (retry1) {
try {
//code to kill Thread
_thread1.join();
retry1 = false;
} catch (InterruptedException e) {
}
}
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
if(_thread1.state1==GameThread1.PAUSED){
//When game is opened again in the Android OS
_thread1 = new GameThread1(getHolder(), _context1, new Handler(),this);
_thread1.start();
}else{
//creating the game Thread for the first time
_thread1.start();
}
}
public void Update() {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:3)
构造函数是一个与您的类(GameSurface1
)同名的方法,并且没有返回值,所以这是一个构造函数: -
public GameSurface1(Context context)
...这是一个没有参数的构造函数: -
public GameSurface1()
答案 1 :(得分:3)
来自维基百科definition:
默认构造函数:
如果程序员没有提供可实例化的构造函数 类,大多数语言都会提供默认构造函数。该 默认构造函数的行为取决于语言。有可能 将数据成员初始化为零或其他相同的值,或者它可能会 什么都没有。在C ++中,如果是数组,则需要默认构造函数 类对象的创建。其他语言(Java,C#,VB .NET)没有这样的限制。
默认构造函数的示例:
public GameSurface1();
参数化构造函数:
可以接受参数的构造函数称为参数化 构造函数。参数的数量可以大于或等于 one(1)。当在参数化构造函数中声明对象时,
初始值必须作为参数传递给构造函数 功能。对象声明的正常方式可能不起作用。该 构造函数可以显式或隐式调用。的方法 隐式调用构造函数也称为速记 方法
参数化构造函数的示例:
public GameSurface1(Context context);