简体   繁体   English

每次我运行Android应用程式时当机

[英]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.

相关问题 检索指纹Blob在运行时使应用程序崩溃,适用于Android的SQLITE - Retrieving a fingerprint Blob is Crashing the app at run time, SQLITE for Android 在java / android +崩溃应用程序中的时间 - Time in java/android + crashing app Android Studio:代码已完成,但是每次我尝试运行时应用崩溃 - Android Studio: Code is done, but app is crashing everytime I try to run Android 聊天应用程序在我运行时不断崩溃 - Android chat app keeps on crashing when I run it Android Calculator App运行时立即崩溃 - Android Calculator App Crashing immediately when run Android Studio,应用程序将无法运行而不会崩溃 - Android Studio, app will not run without crashing 简单的 android 工作室应用程序在我运行时崩溃。 它没有错误 - Simple android studio app is crashing when i run it. it have no errors 我的聊天应用程序在 android 工作室运行时不断崩溃,但没有构建错误 - My chat app keeps crashing, on android studio when i run it but there no build errors 当我运行Android应用程序时,它不会停止崩溃,ParseInt遇到了麻烦 - Android app won't stop crashing when I run it, trouble with ParseInt 从 Playstore 下载时,我的 Android 应用程序崩溃,但是当我将它从 Android Studio 运行到任何设备时,它可以正常工作 - My Android App is crashing when downloaded from the Playstore but when I run it from Android Studio into any device it works normally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM