I'm pretty new to Android Studio. I'm just trying to run a simple pedometer app. No errors are showing up in the code. When I try to run the app on a phone it says it's successful but then nothing else happens. Nothing shows in the logcat. When I run the app in an emulator nothing happens either. Am I missing something?
//MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp.pedometerapp" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation= "portrait"></activity>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</application>
//MAINACTIVITY.JAVA
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
// Display Fields for Accelerometer
private TextView textViewX;
private TextView textViewY;
private TextView textViewZ;
// Display Field 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
protected void onCreate(Bundle savedInstantState) {
super.onCreate(savedInstantState);
setContentView(R.layout.activity_pedometer);
// 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 restButton 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;
// Initialize acceleration Values
acceleration = 0.00f;
// Enable the listener
enableAccelerometerListening();
} // End Method onCreate()
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present
getMenuInflater().inflate(R.menu.menu_main, menu); // change to main
return true;
} // end Method onCreateOptionsMenu
private void enableAccelerometerListening() {
// Initialize the Sensor Manager
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
// Event handler for accelerometer events
private SensorEventListener sensorEventListener =
new SensorEventListener() {
// Listens for Change in Acceleration, Displays, and Computes the steps
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;
} // end of SensorChanged
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Empty - required by Class
} // end on Accuracy Changed
}; // ends private inner class sensorEventListener
// Called by the resetButton to set the Steps count to 0 and reset the Display
public void resetSteps(View v){
numSteps=0;
textViewSteps.setText(String.valueOf(numSteps));
} // End method resetSteps
// the inner class for the seekBarListener
private OnSeekBarChangeListener seekBarListener =
new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser){ // }
// Change the threshold
threshold=seekBar.getProgress();
// Write to the TextView
textSensitive.setText(String.valueOf(threshold));
} // End Method onProgressChanged()
public void onStartTrackingTouch(SeekBar seekBar){
// TODO Auto-generated method stub
} // End Method onStartTrackingTouch()
public void onStopTrackingTouch (SeekBar seekBar){
// TODO Auto-generated method stub
} // End Method onStopTrackingTouch ()
};
}
If anyone can offer any help I will be eternally grateful!