我正在尝试构建一个应用程序,该应用程序将从此链接记录加速度计数据:http: //mobile.tutsplus.com/tutorials/android/android-barometer-logger-acquiring-sensor-data/
问题是我遇到了一些错误,但不明白为什么我会遇到这些错误。我在下面粘贴我的代码:
import java.text.SimpleDateFormat;
import java.util.Date;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity implements SensorEventListener {
SensorManager sensorManager = null;
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
SensorEventListener myListenerInstance = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.activity_main);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
sensorManager.registerListener(myListenerInstance, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
long timestamp = event.timestamp;
float value = event.values[0];
// String res=String.valueOf(event.values[0])+"#"+String.valueOf(event.values[1])+"#"+String.valueOf(event.values[2]);
// Log.d("test", res);
}
protected void onResume(){
super.onResume();
sensorManager.registerListener(myListenerInstance, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onStop(){
sensorManager.unregisterListener(myListenerInstance);
super.onStop();
}
}
我得到的错误在这里:
06-22 21:44:27.322: E/AndroidRuntime(9918): FATAL EXCEPTION: main
06-22 21:44:27.322: E/AndroidRuntime(9918): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rasam.sampleproject/com.rasam.sampleproject.MainActivity}: java.lang.NullPointerException
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.ActivityThread.access$600(ActivityThread.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.os.Handler.dispatchMessage(Handler.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.os.Looper.loop(Looper.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.ActivityThread.main(ActivityThread.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 21:44:27.322: E/AndroidRuntime(9918): at java.lang.reflect.Method.invoke(Method.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at dalvik.system.NativeStart.main(Native Method)
06-22 21:44:27.322: E/AndroidRuntime(9918): Caused by: java.lang.NullPointerException
06-22 21:44:27.322: E/AndroidRuntime(9918): at com.rasam.sampleproject.MainActivity.<init>(MainActivity.java:26)
06-22 21:44:27.322: E/AndroidRuntime(9918): at java.lang.Class.newInstanceImpl(Native Method)
06-22 21:44:27.322: E/AndroidRuntime(9918): at java.lang.Class.newInstance(Class.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.Instrumentation.newActivity(Instrumentation.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): ... 12 more
谁能告诉我为什么会这样???