android studio locationmanager,java - 使用LocationListener和LocationManager的Android Studio项目似乎未运行 - 堆栈内...

在尝试创建一个Android应用程序,该程序通过GPS获取并显示设备速度时遇到问题。代码中实现了LocationListener,但在按钮点击事件中,速度值没有正确显示。可能的错误包括权限管理、LocationManager的使用以及更新UI的逻辑。目前,无论使用从YouTube视频中学到的'ourSpe'还是从Stack Overflow找到的'speed',都无法成功输出速度。
摘要由CSDN通过智能技术生成

我在下面编写并编译了该程序,然后运行了该程序,但其预期目的(即显示textView的速度)无法正常运行,我无法从在手机上运行该程序得知。 有两个位置输出变量: speed和ourSpe因为ourSpe来自我观看的youtube视频,但它不起作用, speed来自我查找的堆栈溢出问题。 两者都有帮助,但我按Button spedButt都没有结果打印出来。 我想我只是以错误的顺序编写了代码,但是我也不确定我是否在正确使用LocationManager 。

布局文件在Relativelayout只有两个textViews和一个Button ,但是堆栈溢出不断给我一个错误,我也无法弄清楚一个。 我今天过得很糟糕。

主要代号

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值