0
我使用此鏈接http://blog.androgames.net/85/android-accelerometer-tutorial/來創建加速度計。但我希望這個加速度計在後臺工作。所以我改變了這樣的代碼:加速度計在後臺服務中打開
public class Accelerometer extends Service implements AccelerometerListener{
private static Context CONTEXT;
@Override
public IBinder onBind(Intent intent) {
// TODO Put your code here
return null;
}
@Override
public void onCreate() {
System.out.println(」start listening」);
// if (AccelerometerManager.isSupported()) {
AccelerometerManager.startListening(this);
// }
}
@Override
public void onDestroy() {
System.out.println(」start listening」);
// if (AccelerometerManager.isListening()) {
AccelerometerManager.stopListening();
// }
}
public static Context getContext() {
return CONTEXT;
}
/**
* onShake callback
*/
public void onShake(float force) {
Toast.makeText(this, 「close」 + force, 1000).show();
}
/**
* onAccelerationChanged callback
*/
public void onAccelerationChanged(float x, float y, float z) {
System.out.println(」x = 「+x+」 y = 「+y+」 z = 「+z);
}
}
我得到這個錯誤:
Java.lang.runtimeexception : Unable to create service com.tam.Accelerometer: java.lang.NullPointerException
Caused by: java;lang.NullPointerException
誰能幫助我?
可以添加整個堆棧跟蹤分配上下文值作爲
? – Augusto