Java Android Studio:某些手机上的应用程序崩溃
我是android开发的新手,对这个问题很困惑。 我正在制作一个简单的应用程序,它有3个文本视图:文本、文本2和文本3Java Android Studio:某些手机上的应用程序崩溃,java,android,android-studio,location,Java,Android,Android Studio,Location,我是android开发的新手,对这个问题很困惑。 我正在制作一个简单的应用程序,它有3个文本视图:文本、文本2和文本3 文本显示用户的纬度 text2显示用户的经度和 text3显示用户与名为“阿扎迪广场”的地方之间的距离 该应用程序在Samsung Galaxy S4和Samsung Galaxy Tab S上正常运行,但在Huawei Y511和Sony Xperias之一(我不知道确切名称)中,该应用程序未打开,并显示该应用程序已不幸停止 这是我的Java代码: public clas
- 文本显示用户的纬度
- text2显示用户的经度和
- text3显示用户与名为“阿扎迪广场”的地方之间的距离
Samsung Galaxy S4
和Samsung Galaxy Tab S
上正常运行,但在Huawei 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;
}
}
注意:我刚刚删除了导入强>
清单文件:
<?xml version="1.0" encoding="utf-8"?>
注意:我在打开应用程序之前打开了手机的位置,所以这不是问题所在。我想说的另一件事是,所有的计算都是正常的,它们工作得很好。在
Android 6.0(API级别23
)和更高版本上,您需要在运行时请求对访问粗略位置
和访问精细位置
的权限。看见可能就是这样。最好的办法是记录问题并回溯,这样你就可以看到问题的发展方向,或者至少有一条线索。如果用户35603的回答不起作用,请尝试此操作,并发布与您的问题相关的logcat部分。最终问题得到解决!这部分代码正在制造麻烦:
Location location = locationManager.getLastKnownLocation(provider);
text.setText(location.getLatitude()+"");
text2.setText(location.getLongitude()+"");
我想location
获取当前位置需要一点时间。实际上,当应用程序想要运行下面的行时,location仍然为null,并且应用程序崩溃:
text1.setText(location.getLatitude()+"");
我用这样的方法解决了车祸:
Location location = locationManager.getLastKnownLocation(provider);
while(location==null){}
text.setText(location.getLatitude()+"");
text2.setText(location.getLongitude() + "");
text3.setText(distanceBetweenMeter(azadiSquare,location)+"");
崩溃解决了,但应用程序冻结在白色屏幕上。我不太清楚,但我猜它被困在
循环中。谢谢 谢谢你这么快回答。但我提到的所有手机都有安卓4或5…谢谢你的回答。我怎样才能在电话里用logcat?我想我应该说,我建立了apk和安装在手机上。所以我不知道如何使用logcat…在stackoverflow上向您展示它的最佳方式是查看这些有用的链接:如果您还没有安装ADB驱动程序,那么您需要安装ADB驱动程序,但我假设您已经安装了ADB驱动程序,因为您有android studio:然后转到该目录,或者如果已使用CMD设置了路径,然后按照google上的链接进行安装使用ADB如果在我第一次发表评论后你不理解,本教程可以让你明白一点,因为stackoverflow不允许太长和详细的故事:实际上我知道如何使用ADB,但问题是ADB没有位置。它会产生任何问题吗?它不会产生任何问题,因为adb只是你的手机和电脑之间的桥梁。如果你成功地记录,你会看到与位置相关的问题会发生在哪里。Goodluck当它在屏幕上显示消息“不幸已停止”时,那里一定有一些日志。在调试模式下在华为y511或索尼Xperia上运行应用程序并获取日志。编辑您的原始问题并粘贴错误。您可以共享应用程序的版本吗?gradle?请解释您的答案,而不是简单地发布代码片段。其他用户应该能够从你的答案中学习。嘿,大卫!这里的问题是当您的位置未打开或您的gps由于低gps信号而无法显示位置时。为了避免这个问题,只需让你的应用程序在ui线程中显示一些进度条,并在后台线程中使用while state语句获取位置,这样ui就不会像在后台线程中一样冻结。很好,但请尝试以解释的方式更新你的答案。谢谢
public class MainActivity extends AppCompatActivity {
pb=findViewById(R.id.progressbar);
Example ex=new Example();//The subclass object is created here
boolean stop=false;//a boolean variable to start or stop the thread to get location.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (location== null) {
showSettingDialog();//this will show your custom way of displaying
//the location setting intent
new Thread(ex).start();//here the fetching location gets started
}
else{
//here write code after your location is fetched
//here to got some other base activity or stay in this
finish();//if you wan to stay in this activity then omit this line.
}
}
public class Example implements Runnable{ //This is a sub class in your main activity.
@Override
public void run() {//overriding run medthod
try {
while(location==null) { //this part keeps on running whithout
//ui freeze
if (stop) {
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
pb.setVisibility(View.VISIBLE);
}
});
getLastLocation();//this function will fetch location.
Thread.sleep(1500);
pb.setVisibility(View.INVISIBLE);
if (location != null) {
//do your code here
stop = true;
finish();
}
}
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
public class MainActivity extends AppCompatActivity {
pb=findViewById(R.id.progressbar);
Example ex=new Example();//The subclass object is created here
boolean stop=false;//a boolean variable to start or stop the thread to get location.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (location== null) {
showSettingDialog();//this will show your custom way of displaying
//the location setting intent
new Thread(ex).start();//here the fetching location gets started
}
else{
//here write code after your location is fetched
//here to got some other base activity or stay in this
finish();//if you wan to stay in this activity then omit this line.
}
}
public class Example implements Runnable{ //This is a sub class in your main activity.
@Override
public void run() {//overriding run medthod
try {
while(location==null) { //this part keeps on running whithout
//ui freeze
if (stop) {
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
pb.setVisibility(View.VISIBLE);
}
});
getLastLocation();//this function will fetch location.
Thread.sleep(1500);
pb.setVisibility(View.INVISIBLE);
if (location != null) {
//do your code here
stop = true;
finish();
}
}
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}