【问题标题】:getSystemServices is undefined when called in a Fragment?在片段中调用 getSystemServices 时未定义?
【发布时间】:2014-06-26 09:53:12
【问题描述】:

我希望TextViewsFragment 中显示传感器读数。当尝试初始化SensorManager getSystemServicesFragment 中未定义时,eclipse 说。为什么以及如何解决它。

片段

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

    }
};

}

【问题讨论】:

    标签: android android-fragments android-sensors


    【解决方案1】:

    再调用一个方法:

    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);  
    

    为什么要调用一个额外的方法?
    提供访问系统服务的getSystemService() 方法来自ContextActivity 扩展 ContextFragment 没有。因此,您首先需要获取对包含FragmentActivity 的引用,然后神奇地检索您想要的系统服务。

    【讨论】:

    • getActivity() 可以在当前未附加的情况下返回 null。虽然这是非常罕见的情况,但它可能会发生。
    【解决方案2】:

    用途:

    getActivity().getSystemService(name)
    

    【讨论】:

      【解决方案3】:

      关于 Kotlin 的使用:

      this.sensorManager = activity!!.getSystemService(Context.SENSOR_SERVICE) as SensorManager
      

      【讨论】:

        【解决方案4】:
        sensorManager = (SensorManager) getActivity().getSystemService(Context.NAMEOF_SERVICE);
        

        Fragment 不能直接调用系统服务,必须使用附加这些 Fragment 的 Activity

        【讨论】:

          【解决方案5】:
          sensorManager = (SensorManager) 
          requireActivity().getSystemService(Context.SENSOR_SERVICE);
          

          【讨论】:

          • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
          【解决方案6】:

          就我而言,这有帮助:

          val manager = context!!.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
          

          【讨论】:

            【解决方案7】:

            您还可以从 LayoutInflater 中获取上下文。

            SensorManager sm = (SensorManager) getLayoutInflater().getContext().getSystemService(Context.SENSOR_SERVICE);
            

            【讨论】:

              【解决方案8】:

              对于 kotlin,这对我有用。

              private fun hideKeyboard() {
                      val inputManager = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                      if (inputManager.isAcceptingText) {
                          inputManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, 0)
                      }
                  }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-04-12
                • 1970-01-01
                • 2014-08-11
                • 1970-01-01
                • 2014-01-20
                相关资源
                最近更新 更多