Android 在 Fragment 中调用 getSystemServices 时未定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24427414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getSystemServices is undefined when called in a Fragment?
提问by user2121
I want TextViews
to display the sensors readings in a Fragment
. When trying to initialize the SensorManager
the getSystemServices
is undefined in the Fragment
, eclipse says.Why and how to fix it.
我想TextViews
在 .csv 文件中显示传感器读数Fragment
。尝试初始化 中SensorManager
的getSystemServices
未定义时Fragment
,eclipse 会说。为什么以及如何修复它。
Fragment
分段
public class FragSensors extends Fragment {
private TextView accXTv, accYTv, accZTv;
private SensorManager sensorManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.frag_sensors, container, false);
accXTv = (TextView) v.findViewById(R.id.accXValue);
accYTv = (TextView) v.findViewById(R.id.accYValue);
accZTv = (TextView) v.findViewById(R.id.accZValue);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
private final SensorEventListener mSensorListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
};
}
}
回答by Little Child
Just one more method call:
还有一个方法调用:
sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
Why that one extra method call?
the getSystemService()
method that provides access to system services comes from Context
. An Activity
extends Context
, a Fragment
does not. Hence, you first need to get a reference to the Activity
in which the Fragment
is contained and then magically retrieve the system service you want.
为什么要调用一个额外的方法?提供访问系统服务
的getSystemService()
方法来自Context
. 一个Activity
扩展Context
,一个Fragment
没有。因此,你首先需要获得一个参考Activity
,其中Fragment
包含然后奇迹般地找回你想要的系统服务。
回答by Zoran
Use:
用:
getActivity().getSystemService(name)
回答by gundrabur
On Kotlin use:
在 Kotlin 上使用:
this.sensorManager = activity!!.getSystemService(Context.SENSOR_SERVICE) as SensorManager
回答by Usman Iqbal
sensorManager = (SensorManager) getActivity().getSystemService(Context.NAMEOF_SERVICE);
Fragments cannot directly call System Services , You have to use Activity with which these Fragment is Attached
Fragment 不能直接调用 System Services ,你必须使用这些 Fragment 附加的 Activity
回答by Jeffrey
You can also get the context from the LayoutInflater.
您还可以从 LayoutInflater 获取上下文。
SensorManager sm = (SensorManager) getLayoutInflater().getContext().getSystemService(Context.SENSOR_SERVICE);
回答by kioa845 - The Samoan Developer
For kotlin this is what worked for me.
对于 kotlin,这对我有用。
private fun hideKeyboard() {
val inputManager = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (inputManager.isAcceptingText) {
inputManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, 0)
}
}
回答by Anik
sensorManager = (SensorManager)
requireActivity().getSystemService(Context.SENSOR_SERVICE);