[英]Android app is crashing each time I run it
I started BLE113 and Android programming. 我开始进行BLE113和Android编程。 I followed the guidelines from this website:
我遵循了该网站的指导原则:
http://www.software7.com/blog/creating-a-beacon-app-for-android-in-less-than-10-minutes-from-scratch/ http://www.software7.com/blog/creating-a-beacon-app-for-android-in-less-than-10-minutes-from-scratch/
Everything was working great when I followed the tutorial. 当我按照教程学习时,一切工作都很好。 Then I started to play with the code.
然后我开始玩代码。 Instead of reading the distance and the UUID on the Android terminal, I decided to read it on the app.
我没有在Android终端上读取距离和UUID,而是决定在应用程序上读取它。 The app is being shutdown each time I run it and getting the following message: “unfortunately, app name has stopped”.
每次我运行该应用程序时都会将其关闭,并显示以下消息:“不幸的是, 应用程序名称已停止”。 I really don't understand why is that.
我真的不明白为什么。 Here is the code when my app crashes:
这是我的应用崩溃时的代码:
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, org.altbeacon.beacon.Region region) {
for (Beacon oneBeacon: beacons) {
// Log.d(Tag, "distance:"+oneBeacon.getDistance() + "id:" +oneBeacon.getId1()+ "/" + oneBeacon.getId2()+"/" + oneBeacon.getId3());
TextView distance = (TextView) findViewById(R.id.distanceText);
distance.setText(""+oneBeacon.getDistance());
}
}
Here is when it doesn't crash: 这是它不会崩溃的时间:
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, org.altbeacon.beacon.Region region) {
for (Beacon oneBeacon: beacons) {
Log.d(Tag, "distance:"+oneBeacon.getDistance() + "id:" +oneBeacon.getId1()+ "/" + oneBeacon.getId2()+"/" + oneBeacon.getId3());
//TextView distance = (TextView) findViewById(R.id.distanceText);
//distance.setText(""+oneBeacon.getDistance());
}
}
Full code is published here: 完整的代码在这里发布:
https://github.com/Boniface316/androidProjects#androidprojects https://github.com/Boniface316/androidProjects#androidprojects
I started Android programming 4 weeks ago and still learning, I really don't know why I am getting this message. 我4周前开始进行Android编程,现在仍在学习,我真的不知道为什么收到此消息。 Any suggestions or feedback is greatly appreciated.
任何建议或反馈,我们将不胜感激。
You should always use the debugging tools to get errors using eclipse or Android Studio. 您应该始终使用调试工具在Eclipse或Android Studio中获取错误。 That is the best way to find the issue
那是找到问题的最好方法
As far as I can see you should remove the initialization of distance
out this method. 据我所知,您应该删除此方法的
distance
初始化。
Declare it as a field: 声明为字段:
private TextView distance;
intialize it in onCreate()
在
onCreate()
初始化它
distance = (TextView) findViewById(R.id.distanceText);
Then 然后
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, org.altbeacon.beacon.Region region) {
for (Beacon oneBeacon: beacons) {
// Log.d(Tag, "distance:"+oneBeacon.getDistance() + "id:" +oneBeacon.getId1()+ "/" + oneBeacon.getId2()+"/" + oneBeacon.getId3());
distance.setText(distance.getText() + " | "+oneBeacon.getDistance());
}
}
Also change the loop code to : 还将循环代码更改为:
distance.setText(distance.getText() + " | "+oneBeacon.getDistance());
Otherwise you would overwrite it 否则你会覆盖它
I don't know about the beacon thing... but as I guess, 我不知道信标的事...但是我猜,
that didRangeBeaconsInRegion method is being called from another thread, which is not main UI thread. 从另一个线程(不是主UI线程)调用didRangeBeaconsInRegion方法。
In android, UI related works are needed to be done in main thread and network related works have to be done in a separate thread. 在android中,与UI相关的工作需要在主线程中完成,而与网络相关的工作则必须在单独的线程中完成。
I guess that beacon thing uses the network connection in another thread and calls this method in its own network thread. 我猜这是信标事物在另一个线程中使用网络连接,并在其自己的网络线程中调用此方法。
Try using handler to send message to main thread for UI update. 尝试使用处理程序将消息发送到主线程以进行UI更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.