Android运行应用程序-代码符合要求但崩溃

Android运行应用程序-代码符合要求但崩溃,android,Android,我最近开始编写Android代码,我尝试使用跟踪步骤、速度和距离的代码。我编译的代码没有错误,但在虚拟电话中打开时崩溃。我不知道为什么会发生这种情况,任何帮助都将不胜感激 package com.example.runnable; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.ComponentName; import

我最近开始编写Android代码,我尝试使用跟踪步骤、速度和距离的代码。我编译的代码没有错误,但在虚拟电话中打开时崩溃。我不知道为什么会发生这种情况,任何帮助都将不胜感激

     package com.example.runnable;

    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.ComponentName;
    import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.LocationManager;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    private TextView textViewX;
    private TextView textViewY;
    private TextView textViewZ;

    private TextView textSensitive;
    private TextView textSteps;
    private Button buttonReset;

    private SensorManager sensorManager;
    private float acceleration;

    private float previousY;
    private float currentY;
    private int numSteps;

    private SeekBar seekBar;
    private double threshold;

    LocationService myService;
    static boolean status;
    LocationManager locationManager;
    static TextView dist, time, speed;
    Button start, pause, stop;
    static long startTime, endTime;
    ImageView image;
    static ProgressDialog locate;
    static int p = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //textViewX = (TextView) findViewById(R.id.textViewX);
        //textViewY = (TextView) findViewById(R.id.textViewY);
        //textViewZ = (TextView) findViewById(R.id.textViewZ);
        textSteps = (TextView) findViewById(R.id.stepsValue);
        //textSensitive = (TextView) findViewById(R.id.textSensitive);
        Button buttonStart = (Button) findViewById(R.id.startBtn);
        threshold = 5.8;
        //textSensitive.setText(String.valueOf(threshold));
        previousY = 0;
        currentY = 0;
        numSteps = 0;
        acceleration = 0.00f;


        buttonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enableAccelerometerListening();
                //The method below checks if Location is enabled on device or not. If not, then an alert dialog box appears with option
                //to enable gps.
                checkGps();
                locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

                if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

                    return;
                }


                if (status == false)
                    //Here, the Location Service gets bound and the GPS Speedometer gets Active.
                    bindService();
                locate = new ProgressDialog(MainActivity.this);
                locate.setIndeterminate(true);
                locate.setCancelable(false);
                locate.setMessage("Getting Location...");
                locate.show();
                start.setVisibility(View.GONE);
                //pause.setVisibility(View.VISIBLE);
                //pause.setText("Pause");
                //stop.setVisibility(View.VISIBLE);


            }
        });
    }


    //This method leads you to the alert dialog box.
    void checkGps() {
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {


            showGPSDisabledAlertToUser();
        }
    }

    //This method configures the Alert Dialog box.
    private void showGPSDisabledAlertToUser() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Enable GPS to use application")
                .setCancelable(false)
                .setPositiveButton("Enable GPS",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent callGPSSettingIntent = new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(callGPSSettingIntent);
                            }
                        });
        alertDialogBuilder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }


    private void enableAccelerometerListening() {
        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) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            currentY = y;

            if (Math.abs(currentY - previousY) > threshold) {
                numSteps++;
                textSteps.setText(String.valueOf(numSteps));
            }

            //textViewX.setText(String.valueOf(x));
            //textViewY.setText(String.valueOf(y));
            //textViewZ.setText(String.valueOf(z));
            previousY = y;
        }

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

        }
    };
    private ServiceConnection sc = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LocationService.LocalBinder binder = (LocationService.LocalBinder) service;
            myService = binder.getService();
            status = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            status = false;
        }
    };

    void bindService() {
        if (status == true)
            return;
        Intent i = new Intent(getApplicationContext(), LocationService.class);
        bindService(i, sc, BIND_AUTO_CREATE);
        status = true;
        startTime = System.currentTimeMillis();
    }

    void unbindService() {
        if (status == false)
            return;
        Intent i = new Intent(getApplicationContext(), LocationService.class);
        unbindService(sc);
        status = false;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onStart() {
        super.onStart();

    }





    // public void resetSteps(View v) {
    //   numSteps = 0;
    // textSteps.setText(String.valueOf(numSteps));
    //}


}

确保您的虚拟设备具有Google API

也许这会帮助你:

使用LogCat检查与崩溃相关的Java堆栈跟踪: