无法访问片段内的SENSOR_SERVICE

时间:2014-06-24 04:41:31

标签: android fragment

这曾经像

sSensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);

在一项活动中,现在是片段我正在制作如下

sSensorManager = (SensorManager)rootView.getContext().getSystemService(SENSOR_SERVICE);

然后我在SENSOR_SERVICE下方获得红线,如下所示

SENSOR_SERVICE cannot be resolved to a variable

2 个答案:

答案 0 :(得分:5)

Fragment

中试试
sSensorManager = (SensorManager)getActivity().getSystemService(getActivity().SENSOR_SERVICE);

答案 1 :(得分:0)

无论您身在何处,都可以像static Constant

那样访问
sSensorManager = (SensorManager)rootView.getContext().getSystemService(Context.SENSOR_SERVICE);