我正在构建一款游戏,当它到达GameScene时会尝试连接耳机。 Project有一个主Activity(名为Activity)和8个独立的类(4个场景扩展了basecene,baseScene类,ResourcesManager和SceneManager);
唯一可行的方法是,如果我在Activity类中拥有所有TG代码,但我需要在GameScene.java中,因为处理程序将设置对象的速度,大小等。 这个错误杀了我
03-25 16:56:04.865 30101-30160/g5.mindwave E/AndroidRuntime﹕ FATAL EXCEPTION: UpdateThread
Process: g5.mindwave, PID: 30101
java.lang.NullPointerException
at g5.mindwave.Activity.connect(Activity.java:57)
at g5.mindwave.GameScene.ThinkGear(GameScene.java:142)
at g5.mindwave.GameScene.createScene(GameScene.java:123)
at g5.mindwave.BaseScene.<init>(BaseScene.java:42)
at g5.mindwave.GameScene.<init>(GameScene.java:34)
at g5.mindwave.SceneManager$1.onTimePassed(SceneManager.java:130)
at org.andengine.engine.handler.timer.TimerHandler.onUpdate(TimerHandler.java:94)
at org.andengine.engine.handler.UpdateHandlerList.onUpdate(UpdateHandlerList.java:47)
at org.andengine.engine.Engine.onUpdateUpdateHandlers(Engine.java:618)
at org.andengine.engine.Engine.onUpdate(Engine.java:605)
at org.andengine.engine.LimitedFPSEngine.onUpdate(LimitedFPSEngine.java:57)
at org.andengine.engine.Engine.onTickUpdate(Engine.java:568)
at org.andengine.engine.Engine$UpdateThread.run(Engine.java:858)
Activity.java:
public void connect(TGDevice tg)
{tg.connect(true);}
GameScene.java:
@Override
public void createScene() {
engine.registerUpdateHandler(new FPSLogger());
engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case TGDevice.MSG_STATE_CHANGE:
Log.v("HelloEEG","State changed");
switch (msg.arg1) {
case TGDevice.STATE_IDLE: break;
case TGDevice.STATE_CONNECTING:
Log.v("HelloEEG","Connecting");
Toast connecting = Toast.makeText(activity.getApplicationContext(),"Connecting", Toast.LENGTH_SHORT);
connecting.show();
break;
case TGDevice.STATE_CONNECTED: tgDevice.start();
Log.v("HelloEEG","Connected");
//TODO Start the wheels spinning, timer, etc.
Toast connected = Toast.makeText(activity.getApplicationContext(),"Connected to "+tgDevice.getConnectedDevice().getName(), Toast.LENGTH_SHORT);
connected.show();
break;
case TGDevice.STATE_DISCONNECTED:
Toast disconnected = Toast.makeText(activity.getApplicationContext(),"Disconnected", Toast.LENGTH_SHORT);
disconnected.show();
break;
case TGDevice.STATE_NOT_FOUND: case TGDevice.STATE_NOT_PAIRED: default:
break;
} break; case TGDevice.MSG_POOR_SIGNAL:
// Log.v("HelloEEG", "PoorSignal: " + msg.arg1);
case TGDevice.MSG_ATTENTION:
Log.v("HelloEEG", "Attention: " + msg.arg1);
break;
case TGDevice.MSG_BLINK:
//Log.v("HelloEEG", "Blinks:" + msg.arg1);
break;
case TGDevice.MSG_MEDITATION:
Log.v("HelloEEG", "Meditation:" +msg.arg1);
rj2.setMotorSpeed(msg.arg1);
// progressMeditation.setProgress(msg.arg1);
break;
case TGDevice.MSG_RAW_DATA:
// int rawValue = msg.arg1;
// Log.v("HelloEEG", "Raw Data: "+ rawValue);
break;
case TGDevice.MSG_EEG_POWER:
TGEegPower ep = (TGEegPower)msg.obj;
//Log.v("HelloEEG", "Delta: " + ep.delta); default:
break;
}
}
};
}
}
);
CreateWorld();
createCar();
SetCamera();
ThinkGear();
}
public void ThinkGear() {
engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
if (resourcesManager.btAdapter != null) {
Log.v("test",resourcesManager.btAdapter.getName());
tgDevice = new TGDevice(btAdapter, handler);
Log.v("tst", tgDevice.toString());
// tgDevice.connect(true);
Log.v("Device", "" + tgDevice.getState());
}
}
});
resourcesManager.activity.connect(tgDevice);
}
答案 0 :(得分:0)
我的愚蠢没有限制,我试图从两个不同的主题连接,这里是我做的:
GameScene.java:
public void createScene() {
engine.registerUpdateHandler(new FPSLogger());
engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case TGDevice.MSG_STATE_CHANGE:
Log.v("HelloEEG","State changed");
switch (msg.arg1) {
case TGDevice.STATE_IDLE: break;
case TGDevice.STATE_CONNECTING:
Log.v("HelloEEG","Connecting");
Toast connecting = Toast.makeText(activity.getApplicationContext(),"Connecting", Toast.LENGTH_SHORT);
connecting.show();
break;
case TGDevice.STATE_CONNECTED: tgDevice.start();
Log.v("HelloEEG","Connected");
//TODO Start the wheels spinning, timer, etc.
Toast connected = Toast.makeText(activity.getApplicationContext(),"Connected to "+tgDevice.getConnectedDevice().getName(), Toast.LENGTH_SHORT);
connected.show();
break;
case TGDevice.STATE_DISCONNECTED:
Toast disconnected = Toast.makeText(activity.getApplicationContext(),"Disconnected", Toast.LENGTH_SHORT);
disconnected.show();
break;
case TGDevice.STATE_NOT_FOUND: case TGDevice.STATE_NOT_PAIRED: default:
break;
} break; case TGDevice.MSG_POOR_SIGNAL:
// Log.v("HelloEEG", "PoorSignal: " + msg.arg1);
case TGDevice.MSG_ATTENTION:
Log.v("HelloEEG", "Attention: " + msg.arg1);
break;
case TGDevice.MSG_BLINK:
//Log.v("HelloEEG", "Blinks:" + msg.arg1);
break;
case TGDevice.MSG_MEDITATION:
Log.v("HelloEEG", "Meditation:" +msg.arg1);
rj2.setMotorSpeed(msg.arg1);
// progressMeditation.setProgress(msg.arg1);
break;
case TGDevice.MSG_RAW_DATA:
// int rawValue = msg.arg1;
// Log.v("HelloEEG", "Raw Data: "+ rawValue);
break;
case TGDevice.MSG_EEG_POWER:
TGEegPower ep = (TGEegPower)msg.obj;
//Log.v("HelloEEG", "Delta: " + ep.delta); default:
break;
}
}
};
if (resourcesManager.btAdapter != null) {
Log.v("test",resourcesManager.btAdapter.getName());
tgDevice = new TGDevice(btAdapter, handler);
Log.v("tst", tgDevice.toString());
tgDevice.connect(true);
Log.v("Device", "" + tgDevice.getState());
}
}
}
);
CreateWorld();
createCar();
SetCamera();
// ThinkGear();
}