为什么app会一直停止?

时间:2017-08-04 17:50:22

标签: java android

我无法弄清楚为什么我的应用会一直停止。我有一个gridlayout,四个按钮排列成两个两个方格,每个按钮都有一个数字。每次按下按钮时,都会生成一个新问题并显示在文本视图中,并为4个按钮分配新编号。以下是我用来分配新数字的方法:

public void assignAnswers(int rightAnswer) {
    int chooseButton = rand.nextInt(4) + 1;
    int answerPossibility = 0;
    Button answerChoice = (Button) answersGridLayout.findViewWithTag("" + chooseButton);
    answerChoice.setText("" + rightAnswer);
    ArrayList<Integer> answersAdded = new ArrayList<>();
    for(int i = 1; i < 5; i++) {
        if(i != chooseButton) {
            Button newAnswerChoice = (Button) answersGridLayout.findViewWithTag("" + i);
            boolean repeat = false;
            do {
                switch (operation) {
                    case "+":
                        answerPossibility = rand.nextInt(41);
                        break;
                    case "-":
                        answerPossibility = rand.nextInt(41) - 20;
                        break;
                    case "*":
                        answerPossibility = rand.nextInt(401);
                        break;
                    case "/":
                        answerPossibility = rand.nextInt(61);
                        break;
                }

                for(int j = 0; j < answersAdded.size(); j++) {
                    if(answerPossibility == answersAdded.get(j) || answerPossibility == rightAnswer) repeat = true;
                }
            } while(repeat);
            answersAdded.add(answerPossibility);
            newAnswerChoice.setText("" + answerPossibility);
        }
    }
}

因此,当我运行我的应用程序时,它可以运行多次,然后停止,按钮不再响应被按下。此外,Android监视器中不显示任何错误。我知道问题必须在上面的方法中,因为当我评论这个方法时,应用程序的其余部分完美运行并且永不停止。此外,rightAnswer已正确计算并从另一个方法返回。 operation也是(它是一个类变量)。

感谢您的帮助。我对这些东西很新,可以使用你所有的输入。

编辑:这就是我认为的堆栈跟踪:

08-04 12:55:07.391 1685-1694/com.android.systemui I/art: Wrote stack traces to '/data/anr/traces.txt'

[ 08-04 12:55:07.404  1245: 1245 W/]
debuggerd: resuming target 1296

[ 08-04 12:55:07.404  1245: 1245 W/]
debuggerd: handling request: pid=1307 uid=1041 gid=1005 tid=1307


[ 08-04 12:55:07.413  1245: 1245 W/]
debuggerd: resuming target 1307

[ 08-04 12:55:07.413  1245: 1245 W/]
debuggerd: handling request: pid=1308 uid=1047 gid=1005 tid=1308


[ 08-04 12:55:07.418  1245: 1245 W/]
debuggerd: resuming target 1308

[ 08-04 12:55:07.419  1245: 1245 W/]
debuggerd: handling request: pid=1310 uid=1019 gid=1019 tid=1310


[ 08-04 12:55:07.422  1245: 1245 W/]
debuggerd: resuming target 1310

[ 08-04 12:55:07.422  1245: 1245 W/]
debuggerd: handling request: pid=1313 uid=1046 gid=1006 tid=1313


[ 08-04 12:55:07.427  1245: 1245 W/]
debuggerd: resuming target 1313

[ 08-04 12:55:07.427  1245: 1245 W/]
debuggerd: handling request: pid=1314 uid=1013 gid=1031 tid=1314


[ 08-04 12:55:07.433  1245: 1245 W/]
debuggerd: resuming target 1314

[ 08-04 12:55:07.433  1245: 1245 W/]
debuggerd: handling request: pid=1315 uid=1040 gid=1026 tid=1315


[ 08-04 12:55:07.437  1245: 1245 W/]
debuggerd: resuming target 1315

[ 08-04 12:55:07.438  1245: 1245 W/]
debuggerd: handling request: pid=1316 uid=1013 gid=1005 tid=1316


[ 08-04 12:55:07.444  1245: 1245 W/]
debuggerd: resuming target 1316

[ 08-04 12:55:07.444  1245: 1245 W/]
debuggerd: handling request: pid=1684 uid=1023 gid=1023 tid=1684


[ 08-04 12:55:07.448  1245: 1245 W/]
debuggerd: resuming target 1684

[ 08-04 12:55:07.448  1245: 1245 W/]
debuggerd: handling request: pid=1760 uid=1023 gid=1023 tid=1760


[ 08-04 12:55:07.452  1245: 1245 W/]
debuggerd: resuming target 1760

08-04 12:55:08.032 1590-1606/system_process E/ActivityManager: ANR in com.saint.speedmathtrainer (com.saint.speedmathtrainer/.MainActivity)
                                                               PID: 23729
                                                               Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago.  Wait queue length: 2.  Wait queue head age: 5789.0ms.)
                                                               Load: 0.36 / 0.17 / 0.22
                                                               CPU usage from 43635ms to 0ms ago (2017-08-04 12:54:23.642 to 2017-08-04 12:55:07.277):
                                                                 3.1% 1296/surfaceflinger: 0% user + 3.1% kernel / faults: 1 minor
                                                                 1.8% 1590/system_server: 0.1% user + 1.7% kernel / faults: 5954 minor
                                                                 1.5% 1300/adbd: 0% user + 1.5% kernel / faults: 20294 minor
                                                                 0.3% 2327/com.google.android.googlequicksearchbox:search: 0% user + 0.2% kernel / faults: 2109 minor
                                                                 0.3% 1307/audioserver: 0% user + 0.3% kernel / faults: 21 minor
                                                                 0.2% 1685/com.android.systemui: 0% user + 0.2% kernel / faults: 219 minor
                                                                 0.1% 1785/com.android.phone: 0% user + 0.1% kernel / faults: 173 minor
                                                                 0.1% 2083/com.google.android.apps.nexuslauncher: 0% user + 0% kernel / faults: 434 minor
                                                                 0% 2006/com.google.android.gms.persistent: 0% user + 0% kernel / faults: 57 minor
                                                                 0% 3/ksoftirqd/0: 0% user + 0% kernel
                                                                 0% 8/rcu_preempt: 0% user + 0% kernel
                                                                 0% 11/migration/1: 0% user + 0% kernel
                                                                 0% 1233/kworker/0:1H: 0% user + 0% kernel
                                                                 0% 1244/logd: 0% user + 0% kernel / faults: 3 minor
                                                                 0% 1294/lmkd: 0% user + 0% kernel
                                                                 0% 1295/servicemanager: 0% user + 0% kernel
                                                                 0% 1318/rild: 0% user + 0% kernel
                                                                 0% 1677/com.android.inputmethod.latin: 0% user + 0% kernel
                                                                 0% 2220/kworker/1:2: 0% user + 0% kernel
                                                                 0% 4672/kworker/0:0: 0% user + 0% kernel
                                                                +0% 23729/com.saint.speedmathtrainer: 0% user + 0% kernel
                                                               23% TOTAL: 18% user + 5% kernel + 0% iowait + 0.3% softirq
                                                               CPU usage from 234ms to 746ms later (2017-08-04 12:55:07.510 to 2017-08-04 12:55:08.022):
                                                                 100% 23729/com.saint.speedmathtrainer: 100% user + 0% kernel
                                                                   102% 23729/peedmathtrainer: 102% user + 0% kernel
                                                                 1.7% 1307/audioserver: 0% user + 1.7% kernel
                                                                 1.9% 1590/system_server: 0% user + 1.9% kernel / faults: 1 minor
                                                                   1.9% 1598/FinalizerDaemon: 1.9% user + 0% kernel
                                                                   1.9% 1606/ActivityManager: 0% user + 1.9% kernel
                                                               52% TOTAL: 50% user + 1.9% kernel
08-04 12:55:08.065 1296-2027/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
08-04 12:55:08.066 1590-29331/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
08-04 12:55:08.066 1590-29331/system_process D/OpenGLRenderer: Swap behavior 1
08-04 12:55:08.066 1590-29331/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-04 12:55:08.066 1590-29331/system_process D/OpenGLRenderer: Swap behavior 0
08-04 12:55:08.076 1296-2027/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
08-04 12:55:08.089 1296-2027/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
08-04 12:55:08.271 1307-1335/? D/AudioFlinger: mixer(0xab6030c0) throttle end: throttle time(1)
08-04 12:55:08.286 1590-29331/system_process D/OpenGLRenderer: endAllActiveAnimators on 0x89de4100 (RippleDrawable) with handle 0x882af950
08-04 12:55:08.477 1590-1613/system_process W/AppOps: Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
08-04 12:55:13.139 2030-24271/com.google.android.gms W/PlatformStatsUtil: Could not retrieve Usage & Diagnostics setting. Giving up.
08-04 13:01:00.964 2006-2006/com.google.android.gms.persistent D/StrictMode: Wrote violation #0 of 5: 15616 bytes
08-04 13:01:00.964 2006-2006/com.google.android.gms.persistent D/StrictMode: Wrote violation #1 of 5: 15640 bytes
08-04 13:01:00.964 2006-2006/com.google.android.gms.persistent D/StrictMode: Wrote violation #2 of 5: 15640 bytes
08-04 13:01:00.964 2006-2006/com.google.android.gms.persistent D/StrictMode: Wrote violation #3 of 5: 15636 bytes
08-04 13:01:00.964 2006-2006/com.google.android.gms.persistent D/StrictMode: Wrote violation #4 of 5: 14632 bytes
08-04 13:01:00.965 2998-3010/com.google.android.gms.unstable D/StrictMode: Wrote violation #0 of 5: 16836 bytes
08-04 13:01:00.965 2998-3010/com.google.android.gms.unstable D/StrictMode: Wrote violation #1 of 5: 16856 bytes
08-04 13:01:00.965 2998-3010/com.google.android.gms.unstable D/StrictMode: Wrote violation #2 of 5: 16856 bytes
08-04 13:01:00.965 2998-3010/com.google.android.gms.unstable D/StrictMode: Wrote violation #3 of 5: 16856 bytes
08-04 13:01:00.965 2998-3010/com.google.android.gms.unstable D/StrictMode: Wrote violation #4 of 5: 15848 bytes
08-04 13:01:01.005 1314-3263/? D/WVCdm: Instantiating CDM.
08-04 13:01:01.006 1314-1333/? I/WVCdm: CdmEngine::OpenSession
08-04 13:01:01.006 1314-1333/? I/WVCdm: Level3 Library 4464 Sep 10 2016 21:49:53
08-04 13:01:01.007 1314-1333/? W/WVCdm: Could not load liboemcrypto.so. Falling back to L3.  dlopen failed: library "liboemcrypto.so" not found
08-04 13:01:01.013 1314-1333/? I/WVCdm: CdmEngine::QueryKeyControlInfo
08-04 13:01:01.013 1314-3236/? I/WVCdm: CdmEngine::GenerateKeyRequest
08-04 13:01:01.013 1314-3236/? D/WVCdm: PrepareKeyRequest: nonce=2676645328
08-04 13:01:01.099 1314-1314/? I/WVCdm: CdmEngine::CloseSession
08-04 13:01:01.103 2006-2666/com.google.android.gms.persistent W/art: Long monitor contention with owner netscheduler-queue-handler (3046) at void rou.a(rpn, int)(:com.google.android.gms:344) waiters=0 in boolean rou.c() for 172ms
08-04 13:01:01.171 2006-29599/com.google.android.gms.persistent I/GoogleURLConnFactory: Using platform SSLCertificateSocketFactory
08-04 13:01:01.174 2006-29599/com.google.android.gms.persistent I/GoogleURLConnFactory: Using platform SSLCertificateSocketFactory
08-04 13:01:01.698 2998-2998/com.google.android.gms.unstable W/Binder:2998_4: type=1400 audit(0.0:18): avc: denied { read } for name="/" dev="tmpfs" ino=3072 scontext=u:r:priv_app:s0:c512,c768 tcontext=u:object_r:device:s0 tclass=dir permissive=0
08-04 13:01:01.804 2006-3111/com.google.android.gms.persistent W/Conscrypt: Could not set socket write timeout: null
08-04 13:01:01.870 2006-3111/com.google.android.gms.persistent W/Conscrypt: Could not set socket write timeout: null
08-04 13:01:01.941 2006-3111/com.google.android.gms.persistent W/GLSUser: [AppCertManager] IOException while requesting key: 
                                                                          java.io.IOException: Invalid device key response.
                                                                              at evk.a(:com.google.android.gms:274)
                                                                              at evk.a(:com.google.android.gms:4238)
                                                                              at evj.a(:com.google.android.gms:45)
                                                                              at evd.a(:com.google.android.gms:50)
                                                                              at evc.a(:com.google.android.gms:104)
                                                                              at com.google.android.gms.auth.account.be.legacy.AuthCronChimeraService.b(:com.google.android.gms:4049)
                                                                              at ecm.call(:com.google.android.gms:2041)
                                                                              at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                              at llt.run(:com.google.android.gms:450)
                                                                              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                              at lqc.run(:com.google.android.gms:17)
                                                                              at java.lang.Thread.run(Thread.java:761)
08-04 13:10:10.118 1590-1654/system_process D/WifiNative-HAL: Failing getSupportedFeatureset because HAL isn't started
08-04 13:10:10.119 1590-1609/system_process E/BluetoothAdapter: Bluetooth binder is null
08-04 13:10:10.119 1590-1609/system_process E/BatteryStatsService: no controller energy info supplied
08-04 13:10:10.122 1590-1609/system_process E/KernelCpuSpeedReader: Failed to read cpu-freq: /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state (No such file or directory)
08-04 13:10:10.123 1590-1609/system_process E/KernelUidCpuTimeReader: Failed to read uid_cputime: /proc/uid_cputime/show_uid_stat (No such file or directory)
08-04 13:10:10.123 1590-1609/system_process E/KernelWakelockReader: neither /proc/wakelocks nor /d/wakeup_sources exists
08-04 13:10:10.123 1590-1609/system_process W/BatteryStatsImpl: Couldn't get kernel wake lock stats
08-04 13:10:10.123 1590-1609/system_process E/BatteryStatsService: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
08-04 13:10:15.195 2030-5935/com.google.android.gms W/PlatformStatsUtil: Could not retrieve Usage & Diagnostics setting. Giving up.
08-04 13:11:00.046 1590-1606/system_process I/ProcessStatsService: Prepared write state in 2ms
08-04 13:11:11.773 2030-6863/com.google.android.gms I/EventLogChimeraService: Aggregate from 1501868471670 (log), 1501868471670 (data)
08-04 13:11:16.914 2030-6865/com.google.android.gms W/PlatformStatsUtil: Could not retrieve Usage & Diagnostics setting. Giving up.

----------------------------------------------- ---------------------------------

更新 我已经解决了我的问题。查看上面的错误日志后,我注意到这一行:

Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago.  Wait queue length: 2.  Wait queue head age: 5789.0ms.)

由此,我得出结论(正确与否,我不确定)我在我的方法中包含了太多循环,以便它能够足够快地工作并跟上我的点击(按钮意味着要按下相对快速的继承)。由于此方法在我的for语句中没有if循环的情况下工作,我将其删除并将其放在单独的方法中,如下所示:

public boolean checkRepeat(ArrayList<Integer> arrayList, int answerP, int answerC) {
    boolean repeatCheck = false;
    for(int j = 0; j < arrayList.size(); j++) {
        if(answerP == arrayList.get(j) || answerP == answerC) repeatCheck = true;
    }
    return repeatCheck;
}

public void assignAnswers(int rightAnswer) {
    int chooseButton = rand.nextInt(4) + 1;
    int answerPossibility = 0;
    Button answerChoice = (Button) answersGridLayout.findViewWithTag("" + chooseButton);
    answerChoice.setText("" + rightAnswer);
    ArrayList<Integer> answersAdded = new ArrayList<>();
    for(int i = 1; i < 5; i++) {
        if(i != chooseButton) {
            Button newAnswerChoice = (Button) answersGridLayout.findViewWithTag("" + i);
            boolean repeat = false;
            do {
                switch (operation) {
                    case "+":
                        answerPossibility = rand.nextInt(41);
                        break;
                    case "-":
                        answerPossibility = rand.nextInt(41) - 20;
                        break;
                    case "*":
                        answerPossibility = rand.nextInt(401);
                        break;
                    case "/":
                        answerPossibility = rand.nextInt(61);
                        break;
                }
                repeat = checkRepeat(answersAdded, answerPossibility, rightAnswer);
            } while(repeat);
            answersAdded.add(answerPossibility);
            newAnswerChoice.setText("" + answerPossibility);
        }
    }
}

所以,我假设在另一种方法中调用一堆方法比简单地将每个方法的所有代码组合到一个方法中更快。任何人都可以确认/限定这一点,也许可以解释为什么会这样?再次感谢。

0 个答案:

没有答案