2016-11-30 63 views
0

我編寫並編譯了下面的程序並運行它,但預期的目的是顯示速度爲textView的功能並沒有發揮作用,據我所知從我的手機上運行它。 有兩個位置輸出變量,speedourSpe,因爲ourSpe來自我觀看的youtube視頻,它不起作用,並且speed來自我擡頭的堆棧溢出問題。兩者都幫助,但都沒有得到一個結果打印出屁股我按下按鈕spedButt。我想我只是寫錯了順序的代碼,但我也不確定我是否在使用LocationManager的權利。使用LocationListener和LocationManager的Android Studio項目似乎沒有運行

佈局文件只有兩個textViews和一個ButtonRelativelayout但堆棧溢出不斷給我一個錯誤,我不能找出一個。我有一個糟糕的一天。

主代碼

package com.example.vitaliy_2.safespeedalert; 

    import android.Manifest; 
    import android.content.Context; 
    import android.content.pm.PackageManager; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.support.v4.app.ActivityCompat; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 



    public class SpeedTest extends AppCompatActivity implements LocationListener { 
      TextView txt; 
      TextView txt_2; 
      Button spedButt; 
      float curSpe; 
      float speed; 
      Location l; 
      Location mLastLocation; 
      Location pCurrentLocation; 

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



     txt = (TextView) findViewById(R.id.speed_display); 
      txt_2 = (TextView) findViewById(R.id.speed_display_2); 
      spedButt = (Button) findViewById(R.id.spedButt); 
      speed = 0; 

     LocationManager lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 

      if (ActivityCompat.checkSelfPermission(this, 
Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 
Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED) {return;} 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
      this.onLocationChanged(null); 

     } 



     @Override 
     public void onLocationChanged(final Location location) { 
      l = location; 
      if (this.mLastLocation != null) 
       speed = (float)Math.sqrt(
         Math.pow(pCurrentLocation.getLongitude() - mLastLocation.getLongitude(), 2) 
           + Math.pow(pCurrentLocation.getLatitude() - mLastLocation.getLatitude(), 2) 
       )/(pCurrentLocation.getTime() - this.mLastLocation.getTime()); 
      this.mLastLocation = pCurrentLocation; 



      spedButt.setOnClickListener(new View.OnClickListener(){ 
       @Override 
       public void onClick(View v) { 
        if(l == null){ 
         txt.setText("-.- m/s"); 
         txt_2.setText("-.- m/s"); 

        }else{ 
         if (pCurrentLocation.hasSpeed()) 


    speed = pCurrentLocation.getSpeed(); 
        curSpe = location.getSpeed(); 

        String sent = speed + "m/s"; 
        txt.setText(sent); 
        String sentTwo = curSpe + "m/s"; 
        txt_2.setText(sentTwo); 
       } 
      } 
     }); 

    } 
+0

您是否要求位置權限? https://developer.android.com/training/permissions/requesting.html – Budius

+0

是@Budius。我在我發佈的代碼中以及在清單中。我確實發現奇怪的是,對話框從來沒有彈出,但程序從來沒有給我一個運行時錯誤。 –

+0

您發佈的代碼只會檢查IF如果您有權限(顯然您沒有),如果IF失敗,您必須請求它。我有點時間在這裏爲你寫信,但再次檢查文檔,它肯定會丟失它 – Budius

回答

0

回答我的問題,該代碼會自動彈出,但仍然需要有要求的許可條款。

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 


     ActivityCompat.requestPermissions(this, 
       new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); 


     return; 
    }else{ 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, this); 
    } 
    this.onLocationChanged(null); 
相關問題