手机上的android如何关闭程序崩溃,Java-Android Studio:某些手机上的应用程序崩溃...

在开发一个Android应用时,开发者遇到一个问题:应用在Samsung Galaxy S4和Samsung Galaxy TabS上运行正常,但在华为Y511和一部Sony Xperia设备上启动时崩溃。应用包含三个TextView,分别显示用户的纬度、经度和到AzadiSquare的距离。问题可能与Android 6.0及以上版本的权限有关,因为这些设备可能需要在运行时请求ACCESS_FINE_LOCATION权限。解决方案是按照Android的运行时权限模型来请求位置权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我是android开发的新手,对此问题感到非常困惑.

我正在制作一个具有3个TextViews的简单应用程序:text,text2和text3.

>文字显示用户的自由度

> text2显示用户的经度,

> text3显示用户与名为“ Azadi Square”的地点之间的距离.

该应用程序可以在Samsung Galaxy S4和Samsung Galaxy Tab S上正常运行,但是在华为Y511和Sony Xperias之一(我不知道确切名称)中,该应用程序无法打开,并表示该应用程序已停止运行

这是我的Java代码:

public class Map extends Activity implements LocationListener {

private TextView text,text2,text3;

private String provider;

private LocationManager locationManager;

private double latitude,longitude;

private Location azadiSquare;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_map);

text = (TextView)findViewById(R.id.text);

text2 = (TextView)findViewById(R.id.text2);

text3 = (TextView)findViewById(R.id.text3);

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria();

provider = locationManager.getBestProvider(criteria,false);

azadiSquare = new Location(provider);

azadiSquare.setLatitude(35.6996540);

azadiSquare.setLongitude(51.3379906);

Location location = locationManager.getLastKnownLocation(provider);

text.setText(location.getLatitude()+"");

text2.setText(location.getLongitude()+"");

text3.setText(distanceBetweenMeter(azadiSquare,location)+"");

}

@Override

protected void onResume() {

super.onResume();

locationManager.requestLocationUpdates(provider,400,1,this);

}

@Override

protected void onPause() {

super.onPause();

locationManager.removeUpdates(this);

}

@Override

public void onLocationChanged(Location location) {

latitude = location.getLatitude();

longitude = location.getLongitude();

text.setText(latitude+"");

text2.setText(longitude+"");

text3.setText(distanceBetweenMeter(azadiSquare,location)+"");

}

@Override

public void onStatusChanged(String s, int i, Bundle bundle) {

// TODO Auto-generated method stub

}

@Override

public void onProviderEnabled(String s) {

Toast.makeText(this,"Enabled new provider " + provider,Toast.LENGTH_LONG).show();

}

@Override

public void onProviderDisabled(String s) {

Toast.makeText(this,"Disabled provider " + provider,Toast.LENGTH_LONG).show();

}

private double getDistanceFromLatLonInKm(double lat1,double lon1,double lat2,double lon2) {

double R = 6371; // Radius of the earth in km

double dLat = deg2rad(lat2-lat1); // deg2rad below

double dLon = deg2rad(lon2-lon1);

double a =

Math.sin(dLat/2) * Math.sin(dLat/2) +

Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *

Math.sin(dLon/2) * Math.sin(dLon/2)

;

double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

double d = R * c; // Distance in km

return d;

}

private double deg2rad(double deg) {

return deg * (Math.PI/180);

}

private int distanceBetweenMeter(Location location1, Location location2){

double distanceKM = getDistanceFromLatLonInKm(location1.getLatitude(),location1.getLongitude(),

location2.getLatitude(),location2.getLongitude());

int distanceMeter = (int)(distanceKM*1000);

return distanceMeter;

}

}

注意:我刚刚删除了导入!

清单文件:

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

注意:打开应用程序之前,我打开了手机的位置,所以这不是问题.我想说的另一件事是,所有计算都可以,并且工作正常.

解决方法:

在Android 6.0(API级别23)及更高版本上,您需要在运行时请求ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION的权限.请参阅Requesting Permissions at Run Time.可能是这种情况.

标签:android-studio,location,java,android

来源: https://codeday.me/bug/20191026/1938361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值