2017-03-15 72 views
0

我最近已經開始編碼Android,即時通訊嘗試使用跟蹤步驟,速度和距離的代碼。我編譯的代碼沒有錯誤,但在虛擬手機中打開時崩潰。林不知道爲什麼會發生這種情況,任何幫助將不勝感激。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)); 
    //} 


} 
+1

使用logcat的檢查與碰撞相關的Java堆棧跟蹤:https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-可以-I-解決,這 – CommonsWare

回答