3

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!

tayleour
  • 31
  • 1
  • 3
  • May be this will help you http://stackoverflow.com/questions/2793956/android-emulator-wont-run-application-started-from-eclipse – Josef Feb 13 '15 at 12:48
  • Try to run a hello-world app first. (When you create a new Android project, Eclipse creates a hello-world app.) Then you can gradually change the app and see when it breaks. – 18446744073709551615 Feb 13 '15 at 12:50
  • What does mean "nothing happens" ? You click the App Icon on your device and it opens, and you only see black screen, or what? – David Kasabji Feb 13 '15 at 12:51
  • @AndroidNFC the app icon doesn't show up on the device. The device still works but the actual app doesn't appear. Will try the hello world app and see where I get. – tayleour Feb 13 '15 at 16:50
  • The Hello World app is working fine. Would I be correct in then thinking it has something to do with my code? – tayleour Feb 13 '15 at 18:15
  • Yes. And if you compile your code, Logcat doesnt give any errors? – David Kasabji Feb 17 '15 at 07:37

0 Answers0