我试图通过单例模式编写代码。
这是我主活动。
public class MainActivity extends AppCompatActivity {
public static MainActivity activity;
public TouchSensor touchSensor;
public SurfaceViewThread surfaceViewThread;
public TriggerChecker triggerChecker;
public Physics physics;
//public static Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity = this;
surfaceViewThread = SurfaceViewThread.getInstance();
touchSensor = TouchSensor.getInstance();
triggerChecker = TriggerChecker.getInstance();
physics = Physics.getInstance();
touchSensor.start();
triggerChecker.start();
physics.start();
//surfaceViewThread is already started.
activity.setContentView(surfaceViewThread);
}
这是我尝试用单例模式编写代码。
public class SurfaceViewThread extends SurfaceView implements Runnable, SurfaceHolder.Callback {
private static SurfaceViewThread surfaceViewThread = new SurfaceViewThread();
private static Camera camera;
private SurfaceHolder holder;
private Thread thread;
private boolean run;
private Vector<ObjectGraphic> objects;
private Vector<Graphic> UIs;
private ObjectGraphic field;
private Vector<ObjectGraphic> draws;
private ComponentMessage componentMessage;
public static SurfaceViewThread getInstance()
{
if(surfaceViewThread == null)
surfaceViewThread = new SurfaceViewThread();
return surfaceViewThread;
}
private SurfaceViewThread()
{
super(activity.getApplicationContext());
camera = new Camera();
getHolder().addCallback(this);
holder = getHolder();
objects = new Vector<>();
UIs = new Vector<>();
draws = new Vector<>();
run = false;
componentMessage = new ComponentMessage();
}
当我构建这段代码时,Camera类因NullPointException而崩溃。
public class Camera extends GameObject {
private Rect area;
private int sight;
private int maxSight = 50;
private int minSight = 10;
private int[] fieldOfView;//{vertical, horizontal}
private int[] maxFieldOfView = new int[] { 60, 45 };
private int[] minFieldOfView = new int[] { 10, 10 };
public Camera()
{
super();
area = new Rect();
this.fieldOfView = new int[] { 60, 45 };
objectMatrix.Rotate(objectMatrix.xVector(), Math.toRadians(-45));
setDisplayVector();
}
private void setSight()
{
double angle = Math.PI/2 - Math.acos(new Common_Math.Matrix1X4(0, 1, 0, 0).Inner(objectMatrix.zVector().Invert()));
if(angle < 0 && angle > - Math.PI/2) {
double t = -objectMatrix.qVector().getY() / objectMatrix.zVector().getY();
int x = (int) (objectMatrix.zVector().getX() * t + objectMatrix.qVector().getX());
int z = (int) (objectMatrix.zVector().getZ() * t + objectMatrix.qVector().getZ());
sight = (int)Math.sqrt(Math.pow((x - objectMatrix.qVector().getX()), 2) + Math.pow((objectMatrix.qVector().getY()), 2) + Math.pow((z - objectMatrix.qVector().getZ()), 2));
if(sight < minSight)
sight = minSight;
}
else
{
sight = maxSight;
}
}
private void setDisplayVector()
{
setSight();
Common_Math.Matrix1X4 upVector, downVector, leftVector, rightVector;
Common_Math.Matrix1X4 zVector = objectMatrix.zVector();
upVector = zVector.Rotate(objectMatrix.xVector(), fieldOfView[0]);
downVector = zVector.Rotate(objectMatrix.xVector(), -fieldOfView[0]);
leftVector = zVector.Rotate(objectMatrix.yVector(), fieldOfView[1]);
rightVector = zVector.Rotate(objectMatrix.yVector(), -fieldOfView[1]);
double angle = Math.PI/2 - Math.acos(new Common_Math.Matrix1X4(0, 1, 0, 0).Inner(objectMatrix.zVector().Invert()));
upVector = upVector.Scaled((int)(Math.abs(objectMatrix.qVector().getY() * Math.tan(angle + fieldOfView[0]))));
downVector = downVector.Scaled((int)(Math.abs(objectMatrix.qVector().getY() * Math.tan(angle - fieldOfView[0]))));
leftVector = leftVector.Scaled((int)(sight * Math.asin(fieldOfView[1])));
rightVector = rightVector.Scaled((int)(sight * Math.asin(fieldOfView[1])));
area.left = (int) objectMatrix.qVector().Add(leftVector).getX();
area.top = (int) objectMatrix.qVector().Add(upVector).getY();
area.right = (int) objectMatrix.qVector().Add(rightVector).getX();
area.bottom = (int) objectMatrix.qVector().Add(downVector).getY();
}
这是游戏对象类
public class GameObject {
protected GameObject upperGameObject;//if upperGameObject is null, this GameObject is parent GameObject.
protected Common_Math.Matrix4X4 objectMatrix;
private Vector<SeaWeedComponent> componentVector;
public GameObject()
{
objectMatrix = new Common_Math.Matrix4X4();
AllocatePhysicsSpace();
componentVector = new Vector<>();
}
和矩阵4x4类
public static class Matrix4X4
{
private Matrix1X4 xVector, yVector, zVector, qVector;
public Matrix4X4() {
xVector = new Matrix1X4(1, 0, 0, 0);
yVector = new Matrix1X4(0, 1, 0, 0);
zVector = new Matrix1X4(0, 0, 1, 0);
qVector = new Matrix1X4(0, 0, 0, 1);
}
所以我检查了一个断点,看看为什么会发生这种情况,但是当我调试它时,每个变量都停留在“正在收集数据..”,什么都没有发生。甚至调试器响应也变慢了。
我猜不出为什么会发生这种情况。我的代码有什么问题吗?我怎么才能看到变量?
3条答案
按热度按时间i2byvkas1#
不确定这应该是评论还是回答...
我在一个完全不同的上下文中看到过同样的问题,它“不知何故”与使用线程有关。似乎当调试器试图获得一个值,并为此需要运行与另一个线程关联的东西时,它会死锁。
在我的例子中,当我需要执行JS时,我有一个在非ui线程上调用的javascript引擎。如果我在任何地方有变量,例如JSObject的子类,调试器就会锁定。
我发现解决这个问题的唯一方法是明智地进行日志记录和设置断点。
检查您的代码在访问变量时是否遇到任何锁或同步语句,这可能是您的情况的原因。
在这个问题得到解决之前,你可以在使用“Gson().toJson(yourObject)"作为json字符串进行调试时看到你的对象。
monwx1rj2#
GameObject.class是做什么的?你的Camera类扩展了GameObject.class,并在构造函数中调用了super()。跳过调试器,使用经典日志记录,在设置变量之前打印输出。
Singleton除了将自身保持为单个示例外,没有做任何特殊的事情,不知道Android Studio调试器如何处理它们,但应该不会有问题。当您尝试示例化Camera.class时,会出现NULL。
尝试在示例化Camera()时捕获NullPointer
stacktrace应该告诉您NullPointer在代码中的位置
62lalag43#
我在创建一个新的虚拟设备来复制我的手机后也遇到了同样的问题。它已经工作了一段时间,然后每次我试图调试它时,它都会在它来到的第一个断点停止工作,并在“收集数据...”
我只是切换回原来的虚拟设备(5. 1 WVGA API 25),调试器开始按照它再次被告知的那样做。
我切换到我的新虚拟设备和问题已经消失了。
可能与我创建的无限循环有关,它抛出了活动快照,但不太确定。如果再次发生,将更新。