问题描述:

My problem is my app force close when I want to register accelerometer listener in onResume().

I used many tutorials and examples and just did what they said. But still I have this problem.

When I remove this line in onResume() the app doesn't force close anymore but still doesn't work.

protected void onResume() {

super.onResume();

senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

}

logcat:

thread exiting with uncaught exception java.lang.NullPointerException at net.OSCam.MainActivity.onSensorChanged

@Override

public void onSensorChanged(SensorEvent arg0) {

Sensor source = arg0.sensor;

float z = arg0.values[2];

if(source.getType() == Sensor.TYPE_ACCELEROMETER){

txtDebug.setText(Float.toString(z));

}

}

网友答案:

Thanks for helping me. I put the line below in onWindowFocusChanged() function instead of onCreate().

txtDebug = (TextView)findViewById(R.id.main_tv_debug);

The function onSensorChanged() runs before onWindowFocusChanged().

相关阅读:
Top