So the below code works on an activity but as soon as i add it to a fragment with onCreateView the app crashes
Can anyone help me rephrase this for a fragment or explain to me what is going wrong?
thanks
public class Exercises extends Activity {
// Display fields for Accelerometer
private TextView textViewX;
private TextView textViewY;
private TextView textViewZ;
// Display Fields for Sensitivity
private TextView textSensitive;
// Display for Steps
private TextView textViewSteps;
// Reset Button
private Button buttonReset;
// Sensor Manager
private SensorManager sensorManager;
private float acceleration;
// Values to Calculate Number of Steps
private float previousY;
private float currentY;
private int numSteps;
// Seekbar Fields
private SeekBar seekBar;
private int threshold; // Point at which we want to trigger a 'step'
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.steps_layout);
try {
SetUpPedometer();
} catch (Exception e) {
e.printStackTrace();
}
}
public void SetUpPedometer(){
// Attach objects to XML View
textViewX=(TextView)findViewById(R.id.textViewX);
textViewY=(TextView)findViewById(R.id.textViewY);
textViewZ=(TextView)findViewById(R.id.textViewZ);
//Attach Step and Sensitive View Objects to XML
textViewSteps=(TextView)findViewById(R.id.textSteps);
textSensitive=(TextView)findViewById(R.id.textSensitive);
//Attach the resetButton to XML
buttonReset = (Button)findViewById(R.id.buttonReset);
//Attach the seekBar to XML
seekBar = (SeekBar)findViewById(R.id.seekBar);
//Set the Values on the seekBar, threshold, and threshold display
seekBar.setProgress(10);
seekBar.setOnSeekBarChangeListener(seekBarListener);
threshold=10;
textSensitive.setText(String.valueOf(threshold));
// Initialize Values
previousY = 0;
currentY = 0;
numSteps =0;
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numSteps = 0;
textViewSteps.setText(String.valueOf(numSteps));
}
});
// Initialize acceleration Values
acceleration = 0.00f;
// Enable the listener - We will write this later in the class
enableAccelerometerListening();
}
private void enableAccelerometerListening(){
//Initialise the Sensor Manager
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(sensorEventListener,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
private SensorEventListener sensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// Gather the values from accelerometer
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
// Fetch the current y
currentY = y;
// Measure if a step is taken
if ( Math.abs(currentY-previousY) > threshold) {
numSteps++;
textViewSteps.setText(String.valueOf(numSteps));
} //end if
// Display the Values
textViewX.setText(String.valueOf(x));
textViewY.setText(String.valueOf(y));
textViewZ.setText(String.valueOf(z));
// Store the previous Y
previousY =y;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
public void buttonReset() {
numSteps = 0;
textViewSteps.setText(String.valueOf(numSteps));
}
private SeekBar.OnSeekBarChangeListener seekBarListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
threshold = seekBar.getProgress();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
}
Fragment public class Steps extends Fragment {
// Display fields for Accelerometer
private TextView textViewX;
private TextView textViewY;
private TextView textViewZ;
// Display Fields for Sensitivity
private TextView textSensitive;
// Display for Steps
private TextView textViewSteps;
// Reset Button
private Button buttonReset;
// Sensor Manager
private SensorManager sensorManager;
private float acceleration;
// Values to Calculate Number of Steps
private float previousY;
private float currentY;
private int numSteps;
// Seekbar Fields
private SeekBar seekBar;
private int threshold; // Point at which we want to trigger a 'step'
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
}
public void SetUpPedometer(View S){
// Attach objects to XML View
textViewX=(TextView)S.findViewById(R.id.textViewX);
textViewY=(TextView)S.findViewById(R.id.textViewY);
textViewZ=(TextView)S.findViewById(R.id.textViewZ);
//Attach Step and Sensitive View Objects to XML
textViewSteps=(TextView)S.findViewById(R.id.textSteps);
textSensitive=(TextView)S.findViewById(R.id.textSensitive);
//Attach the resetButton to XML
buttonReset = (Button)S.findViewById(R.id.buttonReset);
//Attach the seekBar to XML
seekBar = (SeekBar)S.findViewById(R.id.seekBar);
//Set the Values on the seekBar, threshold, and threshold display
seekBar.setProgress(10);
seekBar.setOnSeekBarChangeListener(seekBarListener);
threshold=10;
textSensitive.setText(String.valueOf(threshold));
// Initialize Values
previousY = 0;
currentY = 0;
numSteps =0;
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numSteps = 0;
textViewSteps.setText(String.valueOf(numSteps));
}
});
// Initialize acceleration Values
acceleration = 0.00f;
// Enable the listener - We will write this later in the class
enableAccelerometerListening();
}
private void enableAccelerometerListening(){
//Initialise the Sensor Manager
sensorManager = (SensorManager)getActivity().getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(sensorEventListener,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
private SensorEventListener sensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// Gather the values from accelerometer
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
// Fetch the current y
currentY = y;
// Measure if a step is taken
if ( Math.abs(currentY-previousY) > threshold) {
numSteps++;
textViewSteps.setText(String.valueOf(numSteps));
} //end if
// Display the Values
textViewX.setText(String.valueOf(x));
textViewY.setText(String.valueOf(y));
textViewZ.setText(String.valueOf(z));
// Store the previous Y
previousY =y;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
public void buttonReset() {
numSteps = 0;
textViewSteps.setText(String.valueOf(numSteps));
}
private SeekBar.OnSeekBarChangeListener seekBarListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
threshold = seekBar.getProgress();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View S = inflater.inflate(R.layout.steps_layout, container, false);
try {
SetUpPedometer(S);
} catch (Exception e) {
e.printStackTrace();
}
return S;
}
}
12-13 15:17:15.062 984-984/com.stephenh.daytrack.daytrackstephenh E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.stephenh.daytrack.daytrackstephenh, PID: 984 java.lang.NullPointerException at com.stephenh.daytrack.daytrackstephenh.SwipeViewsAdapter.Steps.SetUpPedometer(Steps.java:101) at com.stephenh.daytrack.daytrackstephenh.SwipeViewsAdapter.Steps.onCreateView(Steps.java:188) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486) at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163) at android.support.v4.view.ViewPager.populate(ViewPager.java:1073) at android.support.v4.view.ViewPager.populate(ViewPager.java:919) at android.support.v4.view.ViewPager$3.run(ViewPager.java:249) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:543) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:743) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5212) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) at dalvik.system.NativeStart.main(Native Method)