Pedometer in fragment

824 Views Asked by At

I am trying to use the pedometer sensor in a fragment to count the number of steps that has been taken since the device last rebooted.

I can get the pedometer working in an Activity but cannot seem to get it working in the fragment. I am new to using fragments so it's probably very simple what I am doing wrong.

My class is below:

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import static com.example.application.placepicker.R.id.count;


public class Tab3 extends Fragment implements SensorEventListener {

private SensorManager sensorManager;
private TextView textView;
boolean activityRunning;
private Button buttonReturn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab3, container, false);
    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    count = (TextView) getActivity().findViewById(count);  **error

    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);     **error

}

@Override
public void onResume() {
    super.onResume();
    activityRunning = true;
    Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    if (countSensor != null) {
        sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
    } else {
        Toast.makeText(this, "Count sensor not available!", Toast.LENGTH_LONG).show();                            **error
    }

}

@Override
public void onPause() {
    super.onPause();
    activityRunning = false;
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (activityRunning) {
        count.setText(String.valueOf(event.values[0]));         **error
    }

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}

My XML file to accompany this is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.application.placepicker.TabbedActivity$PlaceholderFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="114dp"
    android:gravity="center"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true">

    <TextView
        android:id="@+id/textLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Step counter"
        android:textSize="40sp"
        android:textColor="@color/colorAccent"
        android:fontFamily="sans-serif"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="33dp"
        android:layout_gravity="center_horizontal"
        android:textSize="26dp"
        android:text="Steps walked so far today:"
        android:textColor="@color/colorAccent" />

    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="50dp"
        android:text="---"
        android:textColor="@color/colorAccent"
        android:textSize="36dp" />

</LinearLayout>

2

There are 2 best solutions below

4
On

You have no member variable for count

private SensorManager sensorManager;
private TextView textView;
boolean activityRunning;
private Button buttonReturn;

are examples of member variables in your fragment class

count = (TextView) getActivity().findViewById(count);

is setting a TextView to an unknown reference, please initialise count properly i.e.

TextView count = (TextView) getActivity().findViewById(count);

as a function member or:

private TextView count;

with the other class member variables

you will then need to call

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

as a fragment has no reference to the getSystemService method by itself, so it needs to go through it's holding activity

0
On

as well i know you should change

 count = (TextView) getActivity().findViewById(count);

to

count = (TextView)getView().findViewById(R.id.count);

cause you have to define view , don't call activity.