0
傳感器服務出現錯誤。我很確定這是正確的語法,除非我錯過了一些東西。只需要那部分加速度計。非常感謝您是否可以發表想法。Sensor_Service不能作爲變量解析
public class AskQuestions extends Fragment {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeDetector mShakedetector;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_questions, container, false);
//Error
mSensorManager = getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mShakedetector = new ShakeDetector(new OnShakeListener() {
@Override
public void onShake() {
// TODO Auto-generated method stub
handleNewAnswer();
}
});
return rootView;
}
@Override
public void onResume(){
super.onResume();
mSensorManager.registerListener(mShakedetector, mAccelerometer,
SensorManager.SENSOR_DELAY_UI);
}
@Override
public void onPause(){
super.onPause();
mSensorManager.unregisterListener(mShakedetector);
}
private void handleNewAnswer() {
if(makeMethink == true) {
result = answer.makeMeThink();
}else if(WhatsTheAnswer == true){
result = answer.whatsTheAnswer();
} else if (inspireMe){
result = answer.inspireQuotes();
}
displayAnswer.setText(result);
}
}
請解釋爲什麼你的方法解決了OP的問題。 – mavrosxristoforos
添加了解釋。 –