Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getSystemServices is undefined when called in a Fragment?

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.

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

    }
};

}

like image 974
user2121 Avatar asked Jun 26 '14 09:06

user2121


People also ask

How do I get system services from a fragment?

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.

Why can't I run the getsystemservice function from an activity?

It's cause the metod getSystemService belongs to the class Context, so you have to run it on a context but you're running it from an activity.

How to get the activity that contains the fragment?

You can get the activity that contains the fragment with getActivity (). Therefore getActivity ().getApplicationContext () would work. getActivity ().findViewById (int) would also work.


4 Answers

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.

like image 65
An SO User Avatar answered Sep 18 '22 17:09

An SO User


Use:

getActivity().getSystemService(name) 
like image 35
Zoran Avatar answered Sep 19 '22 17:09

Zoran


On Kotlin use:

this.sensorManager = activity!!.getSystemService(Context.SENSOR_SERVICE) as SensorManager
like image 31
gundrabur Avatar answered Sep 16 '22 17:09

gundrabur


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

Fragments cannot directly call System Services , You have to use Activity with which these Fragment is Attached

like image 24
Muhammad Usman Avatar answered Sep 19 '22 17:09

Muhammad Usman