0

在 Android 中,我正在创建一个服务,它将监听传感器值的变化。但是每当我打开它时,我的应用程序就会不断崩溃。如果我删除线条

 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

它工作正常

我创建了如下服务类:

public class CustomSensorService extends Service implements SensorEventListener{

    static SensorManager sensorManager;
    static Sensor mAccelerometer;
    static Context mContext;
    static final String LOG_TAG = "SimpleService";

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this,"Service Created",Toast.LENGTH_SHORT).show();
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Log.d( LOG_TAG, "onSensorChanged" );
        Toast.makeText(mContext,"Heelooo" , Toast.LENGTH_SHORT).show();

        stopSelf();


    }

}

我的活动为:

public class MainActivity extends Activity {

    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intent = new Intent(this, CustomSensorService.class);
    }
    @Override 
    public void onResume(){
        super.onResume();
        startService(intent);  

    }
}

这是清单

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.learnandroidservice.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".CustomSensorService"/>
    </application>

痕迹: 在此处输入图像描述

4

2 回答 2

1

mContext为 null - 您需要在将其传递给 . 之前对其进行初始化Toast.makeText()

Service是一个上下文,所以你可以打电话

Toast.makeText(this,"Heelooo" , Toast.LENGTH_SHORT).show(); 

当您删除您提到的行时它会起作用,因为这样 SensorListener 永远不会被注册,onSensorChanged也永远不会被调用。

于 2013-10-10T12:42:10.810 回答
1

在 CustomSensorService 类中: onSensorChanged() 方法内部:您使用了 mContext 变量值而没有对其进行初始化。

您的代码: Toast.makeText(mContext,"Heelooo" , Toast.LENGTH_SHORT).show();

为解决此问题所做的更改:

Toast.makeText(this, "Heelooo", Toast.LENGTH_SHORT).show();

由于上述原因,您在显示 toast 时收到 NullPointerException。

于 2013-10-10T12:54:42.923 回答