-1

我正在尝试构建一个应用程序,该应用程序将从此链接记录加速度计数据: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

谁能告诉我为什么会这样???

4

1 回答 1

0
SensorManager sensorManager = null;
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

将 sensorManager 初始化为 null 后,立即尝试调用其方法之一,这只会导致空指针异常。

在尝试使用 sensorManager 之前,您必须确保始终执行后面的初始化 sensorManager 的代码行:

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

可能您应该在类的顶部也将传感器初始化为 null,然后让自己成为一个真正的 sensorManager,然后仅在您的 onCreate() 方法中获得一个真正的传感器。

SensorManager sensorManager = null;
Sensor sensor = null;
SensorEventListener myListenerInstance = null;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    setContentView(R.layout.activity_main);
}
于 2013-06-22T18:22:14.147 回答