【问题标题】:App not responding, unable to finish activity应用无响应,无法完成活动
【发布时间】:2014-07-27 00:11:00
【问题描述】:

我有一个游戏的三个活动。第一个是菜单,第二个是游戏活动,最后是显示玩家得分的游戏结束页面。当我进入最后一个活动时,按下后退按钮会使应用程序无响应。

我在 LogCat 中收到一条异常消息:

07-26 19:09:30.756: I/dalvikvm(3223): threadid=3: reacting to signal 3
07-26 19:09:31.396: I/dalvikvm(3223): Wrote stack traces to '/data/anr/traces.txt'

我打开了这个堆栈跟踪文件,但由于文件很长,我无法查明任何错误。

在搜索此消息时,我发现了两个与之相关的 SO 问题;一个没有答案,另一个没有明确的解决方案。

以下是我认为相关的代码部分:

第一个活动只是在 onCreate 中设置布局并包含一个开始按钮的事件侦听器,所以这不是问题。

第二个活动开始游戏循环。要结束这个活动并开始最后一个活动,我使用以下方法:

public void endGame() {
    scheduleTaskExecutor.shutdownNow();

    timer.cancel();
    timer.purge();

    SharedPreferences settings = context.getSharedPreferences(EndGameActivity.PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();

    if (level > settings.getInt("highLevel", 0)) {
        editor.putInt("highLevel", level);
        editor.commit();
    }

    if (zombiesKilled > settings.getInt("highscore", 0)) {
        editor.putInt("highscore", zombiesKilled);
        editor.commit();
    }

    Looper.prepare();

    Handler handler = new Handler();
    handler.post(new Runnable() {
        public void run() {
             GameActivity.activity.finish();
             Intent intent = new Intent(context, EndGameActivity.class);
             context.startActivity(intent);
        }
    });
    Looper.loop();
}

ScheduledThreadPoolExecutor 开启新关卡,因此可以关闭。计时器用于生成和渲染新敌人,因此也应该取消它。

线程在其对应的SurfaceView 中通过surfaceDestroyed 停止。

在我的最后一个活动中,我使用SharedPreferences 提取endGame 方法中看到的两个值并将它们设置为TextViews:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.endgame_layout);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    highscore = settings.getInt("highscore", 0);

    highscoreText = (TextView) findViewById(R.id.highscore);
    highscoreText.setText("Most zombies ever killed is " + highscore);

    totalZombiesKilled = (TextView) findViewById(R.id.totalZombiesKilled);
    totalZombiesKilled.setText("This time you killed " + GameLoop.zombiesKilled);

    highLevel = settings.getInt("highLevel", 0);

    highLevelText = (TextView) findViewById(R.id.highLevelText);
    highLevelText.setText("Your highest level is " + highLevel);

    sessionLevelText = (TextView) findViewById(R.id.score);
    sessionLevelText.setText("This time you survived " + GameLoop.level + " levels");
}

应该注意的是,如果我在没有开始最终活动的情况下结束游戏活动,则一切正常,我会毫无问题地返回菜单。只有当我开始最后的活动然后尝试按后退按钮转到菜单时才会出现问题。

如果您需要任何其他帮助我,请通知我,我会发布您需要的任何内容。

这是堆栈跟踪:

    "RILReceiver" prio=5 tid=12 NATIVE
  | group="main" sCount=1 dsCount=0 obj=0xb2dd5aa0 self=0xb71c8db0
  | sysTid=579 nice=0 sched=0/0 cgrp=apps handle=-1222864376
  | state=S schedstat=( 530000000 2610000000 446 ) utm=43 stm=10 core=0
  #00  pc 00021268  /system/lib/libc.so (recvmsg+8)
  #01  pc 0006bcbd  /system/lib/libandroid_runtime.so
  #02  pc 0006c12d  /system/lib/libandroid_runtime.so
  #03  pc 0001dbcc  /system/lib/libdvm.so (dvmPlatformInvoke+112)
  #04  pc 0004deff  /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+394)
  #05  pc 0003873d  /system/lib/libdvm.so (dvmCheckCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+8)
  #06  pc 00026fe0  /system/lib/libdvm.so
  #07  pc 0002df34  /system/lib/libdvm.so (dvmMterpStd(Thread*)+76)
  #08  pc 0002b5cc  /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184)
  #09  pc 00060319  /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+336)
  #10  pc 0006033d  /system/lib/libdvm.so (dvmCallMethod(Thread*, Method const*, Object*, JValue*, ...)+20)
  #11  pc 0005502b  /system/lib/libdvm.so
  #12  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #13  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)
  at android.net.LocalSocketImpl.readba_native(Native Method)
  at android.net.LocalSocketImpl.access$400(LocalSocketImpl.java:33)
  at android.net.LocalSocketImpl$SocketInputStream.read(LocalSocketImpl.java:98)
  at com.android.internal.telephony.RIL.readRilMessage(RIL.java:421)
  at com.android.internal.telephony.RIL.access$400(RIL.java:215)
  at com.android.internal.telephony.RIL$RILReceiver.run(RIL.java:521)
  at java.lang.Thread.run(Thread.java:841)

"RILSender" prio=5 tid=11 NATIVE
  | group="main" sCount=1 dsCount=0 obj=0xb2dd3560 self=0xb71c7180
  | sysTid=578 nice=0 sched=0/0 cgrp=apps handle=-1222875912
  | state=S schedstat=( 370000000 2140000000 714 ) utm=18 stm=19 core=0
  #00  pc 000215cc  /system/lib/libc.so (epoll_wait+12)
  #01  pc 00010627  /system/lib/libutils.so (android::Looper::pollInner(int)+98)
  #02  pc 00010851  /system/lib/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+92)
  #03  pc 00069ff1  /system/lib/libandroid_runtime.so (android::NativeMessageQueue::pollOnce(_JNIEnv*, int)+22)
  #04  pc 0001dbcc  /system/lib/libdvm.so (dvmPlatformInvoke+112)
  #05  pc 0004deff  /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+394)
  #06  pc 0003873d  /system/lib/libdvm.so (dvmCheckCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+8)
  #07  pc 00000214  /dev/ashmem/dalvik-jit-code-cache (deleted)
  at android.os.MessageQueue.nativePollOnce(Native Method)
  at android.os.MessageQueue.next(MessageQueue.java:138)
  at android.os.Looper.loop(Looper.java:123)
  at android.os.HandlerThread.run(HandlerThread.java:61)

"Binder_2" prio=5 tid=10 NATIVE
  | group="main" sCount=1 dsCount=0 obj=0xb2db7a90 self=0xb71ad658
  | sysTid=552 nice=0 sched=0/0 cgrp=apps handle=-1222978544
  | state=S schedstat=( 370000000 1570000000 660 ) utm=26 stm=11 core=0
  #00  pc 00020418  /system/lib/libc.so (__ioctl+8)
  #01  pc 0002c0ef  /system/lib/libc.so (ioctl+14)
  #02  pc 0001d375  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+140)
  #03  pc 0001da7f  /system/lib/libbinder.so (android::IPCThreadState::getAndExecuteCommand()+6)
  #04  pc 0001db15  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+48)
  #05  pc 000218dd  /system/lib/libbinder.so
  #06  pc 0000ea45  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+216)
  #07  pc 0004cd79  /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+68)
  #08  pc 0000e577  /system/lib/libutils.so
  #09  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #10  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)
  at dalvik.system.NativeStart.run(Native Method)

"Binder_1" prio=5 tid=9 NATIVE
  | group="main" sCount=1 dsCount=0 obj=0xb2db79a0 self=0xb71ac9e8
  | sysTid=551 nice=0 sched=0/0 cgrp=apps handle=-1222981728
  | state=S schedstat=( 440000000 5620000000 682 ) utm=27 stm=17 core=0
  #00  pc 00020418  /system/lib/libc.so (__ioctl+8)
  #01  pc 0002c0ef  /system/lib/libc.so (ioctl+14)
  #02  pc 0001d375  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+140)
  #03  pc 0001da7f  /system/lib/libbinder.so (android::IPCThreadState::getAndExecuteCommand()+6)
  #04  pc 0001db15  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+48)
  #05  pc 000218dd  /system/lib/libbinder.so
  #06  pc 0000ea45  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+216)
  #07  pc 0004cd79  /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+68)
  #08  pc 0000e577  /system/lib/libutils.so
  #09  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #10  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)
  at dalvik.system.NativeStart.run(Native Method)

"FinalizerWatchdogDaemon" daemon prio=5 tid=8 WAIT
  | group="system" sCount=1 dsCount=0 obj=0xb2db3e10 self=0xb71ab3c8
  | sysTid=539 nice=0 sched=0/0 cgrp=apps handle=-1222985696
  | state=S schedstat=( 0 100000000 55 ) utm=0 stm=0 core=0
  at java.lang.Object.wait(Native Method)
  - waiting on <0xb2b17370> (a java.lang.Daemons$FinalizerWatchdogDaemon)
  at java.lang.Object.wait(Object.java:364)
  at java.lang.Daemons$FinalizerWatchdogDaemon.waitForObject(Daemons.java:230)
  at java.lang.Daemons$FinalizerWatchdogDaemon.run(Daemons.java:207)
  at java.lang.Thread.run(Thread.java:841)

"FinalizerDaemon" daemon prio=5 tid=7 WAIT
  | group="system" sCount=1 dsCount=0 obj=0xb2db3c60 self=0xb71aaa98
  | sysTid=538 nice=0 sched=0/0 cgrp=apps handle=-1222988048
  | state=S schedstat=( 100000000 360000000 205 ) utm=7 stm=3 core=0
  at java.lang.Object.wait(Native Method)
  - waiting on <0xb2b02718> (a java.lang.ref.ReferenceQueue)
  at java.lang.Object.wait(Object.java:401)
  at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:102)
  at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:73)
  at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170)
  at java.lang.Thread.run(Thread.java:841)

"ReferenceQueueDaemon" daemon prio=5 tid=6 WAIT
  | group="system" sCount=1 dsCount=0 obj=0xb2db3af8 self=0xb71aa168
  | sysTid=537 nice=0 sched=0/0 cgrp=apps handle=-1222990400
  | state=S schedstat=( 40000000 250000000 208 ) utm=1 stm=3 core=0
  at java.lang.Object.wait(Native Method)
  - waiting on <0xb2b02640> 
  at java.lang.Object.wait(Object.java:364)
  at java.lang.Daemons$ReferenceQueueDaemon.run(Daemons.java:130)
  at java.lang.Thread.run(Thread.java:841)

"Compiler" daemon prio=5 tid=5 VMWAIT
  | group="system" sCount=1 dsCount=0 obj=0xb2db3a08 self=0xb71a9d10
  | sysTid=536 nice=0 sched=0/0 cgrp=apps handle=-1223997408
  | state=S schedstat=( 1160000000 3970000000 1288 ) utm=62 stm=54 core=0
  #00  pc 00021794  /system/lib/libc.so (__futex_syscall3+8)
  #01  pc 0000ed94  /system/lib/libc.so (__pthread_cond_timedwait_relative+48)
  #02  pc 0000edf4  /system/lib/libc.so (__pthread_cond_timedwait+64)
  #03  pc 000739bb  /system/lib/libdvm.so
  #04  pc 0005470d  /system/lib/libdvm.so
  #05  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #06  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)
  at dalvik.system.NativeStart.run(Native Method)

"JDWP" daemon prio=5 tid=4 VMWAIT
  | group="system" sCount=1 dsCount=0 obj=0xb2db3920 self=0xb70b41c0
  | sysTid=534 nice=0 sched=0/0 cgrp=apps handle=-1224000640
  | state=S schedstat=( 70000000 280000000 28 ) utm=5 stm=2 core=0
  #00  pc 00020568  /system/lib/libc.so (select+20)
  #01  pc 00061227  /system/lib/libdvm.so
  #02  pc 00063d65  /system/lib/libdvm.so
  #03  pc 0005470d  /system/lib/libdvm.so
  #04  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #05  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)
  at dalvik.system.NativeStart.run(Native Method)

"Signal Catcher" daemon prio=5 tid=3 RUNNABLE
  | group="system" sCount=0 dsCount=0 obj=0xb2db3828 self=0xb70b3500
  | sysTid=533 nice=0 sched=0/0 cgrp=apps handle=-1224004304
  | state=R schedstat=( 1230000000 2830000000 460 ) utm=65 stm=58 core=0
  at dalvik.system.NativeStart.run(Native Method)

"GC" daemon prio=5 tid=2 VMWAIT
  | group="system" sCount=1 dsCount=0 obj=0xb2db3748 self=0xb70b2880
  | sysTid=532 nice=0 sched=0/0 cgrp=apps handle=-1224241520
  | state=S schedstat=( 550000000 2150000000 7279 ) utm=4 stm=51 core=0
  #00  pc 00021794  /system/lib/libc.so (__futex_syscall3+8)
  #01  pc 0000ed94  /system/lib/libc.so (__pthread_cond_timedwait_relative+48)
  #02  pc 0000edf4  /system/lib/libc.so (__pthread_cond_timedwait+64)
  #03  pc 0007243f  /system/lib/libdvm.so
  #04  pc 0005470d  /system/lib/libdvm.so
  #05  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #06  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)
  at dalvik.system.NativeStart.run(Native Method)

----- end 528 -----


----- pid 53 at 2014-07-26 18:52:05 -----
Cmd line: /system/bin/mediaserver

"mediaserver" sysTid=53
  #00  pc 00020418  /system/lib/libc.so (__ioctl+8)
  #01  pc 0002c0ef  /system/lib/libc.so (ioctl+14)
  #02  pc 0001d375  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+140)
  #03  pc 0001da7f  /system/lib/libbinder.so (android::IPCThreadState::getAndExecuteCommand()+6)
  #04  pc 0001db15  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+48)
  #05  pc 00001b93  /system/bin/mediaserver
  #06  pc 0000e23b  /system/lib/libc.so (__libc_init+50)
  #07  pc 000015e8  /system/bin/mediaserver

"mediaserver" sysTid=112
  #00  pc 00020158  /system/lib/libc.so (read+12)
  #01  pc 00014117  /system/lib/hw/camera.goldfish.so (android::EmulatedCameraHotplugThread::threadLoop()+50)
  #02  pc 0000ea45  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+216)
  #03  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #04  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)

"ApmTone" sysTid=114
  #00  pc 00021798  /system/lib/libc.so (__futex_syscall3+12)
  #01  pc 0000ed94  /system/lib/libc.so (__pthread_cond_timedwait_relative+48)
  #02  pc 00019d91  /system/lib/libaudioflinger.so
  #03  pc 00027a2b  /system/lib/libaudioflinger.so
  #04  pc 0000ea45  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+216)
  #05  pc 0000e577  /system/lib/libutils.so
  #06  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #07  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)

"ApmAudio" sysTid=115
  #00  pc 00021798  /system/lib/libc.so (__futex_syscall3+12)
  #01  pc 0000ed94  /system/lib/libc.so (__pthread_cond_timedwait_relative+48)
  #02  pc 00019d91  /system/lib/libaudioflinger.so
  #03  pc 00027a2b  /system/lib/libaudioflinger.so
  #04  pc 0000ea45  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+216)
  #05  pc 0000e577  /system/lib/libutils.so
  #06  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #07  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)

"ApmOutput" sysTid=116
  #00  pc 00021798  /system/lib/libc.so (__futex_syscall3+12)
  #01  pc 0000ed94  /system/lib/libc.so (__pthread_cond_timedwait_relative+48)
  #02  pc 00019d91  /system/lib/libaudioflinger.so
  #03  pc 00027a2b  /system/lib/libaudioflinger.so
  #04  pc 0000ea45  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+216)
  #05  pc 0000e577  /system/lib/libutils.so
  #06  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #07  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)

"AudioOut_2" sysTid=119
  #00  pc 00021794  /system/lib/libc.so (__futex_syscall3+8)
  #01  pc 0000ed94  /system/lib/libc.so (__pthread_cond_timedwait_relative+48)
  #02  pc 0000edf4  /system/lib/libc.so (__pthread_cond_timedwait+64)
  #03  pc 00019d59  /system/lib/libaudioflinger.so
  #04  pc 0001bad1  /system/lib/libaudioflinger.so
  #05  pc 0000ea45  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+216)
  #06  pc 0000e577  /system/lib/libutils.so
  #07  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #08  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)

"Binder_1" sysTid=121
  #00  pc 00020418  /system/lib/libc.so (__ioctl+8)
  #01  pc 0002c0ef  /system/lib/libc.so (ioctl+14)
  #02  pc 0001d375  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+140)
  #03  pc 0001da7f  /system/lib/libbinder.so (android::IPCThreadState::getAndExecuteCommand()+6)
  #04  pc 0001db15  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+48)
  #05  pc 000218dd  /system/lib/libbinder.so
  #06  pc 0000ea45  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+216)
  #07  pc 0000e577  /system/lib/libutils.so
  #08  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #09  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)

"Binder_2" sysTid=428
  #00  pc 00020418  /system/lib/libc.so (__ioctl+8)
  #01  pc 0002c0ef  /system/lib/libc.so (ioctl+14)
  #02  pc 0001d375  /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+140)
  #03  pc 0001da7f  /system/lib/libbinder.so (android::IPCThreadState::getAndExecuteCommand()+6)
  #04  pc 0001db15  /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+48)
  #05  pc 000218dd  /system/lib/libbinder.so
  #06  pc 0000ea45  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+216)
  #07  pc 0000e577  /system/lib/libutils.so
  #08  pc 0000d060  /system/lib/libc.so (__thread_entry+72)
  #09  pc 0000d1f8  /system/lib/libc.so (pthread_create+240)

【问题讨论】:

  • 发布 /data/anr/traces.txt 的内容,或者至少是其中的最终崩溃。您在 C 代码中崩溃了,该文件将提供更多详细信息。
  • @GabeSechan 里面有超过 2,000 行...你想让我把所有的都贴出来吗...?
  • 不,最后一次崩溃就足够了。但是该文件包含您设备上的 C 崩溃日志,如果没有崩溃,我们真的不能说比某些 C 组件(甚至可能是操作系统组件)崩溃更多。如果你不能告诉,发布最后 200 行左右,我们会弄清楚并在需要时要求更多。
  • @GabeSechan 我发布了最后几百行......抱歉,但我对 C 或这个堆栈跟踪文件一无所知。
  • 我需要你再往上走一点。寻找一条写着 SIGSEGV 或类似内容的行。那应该是崩溃的第一行。

标签: java android multithreading android-activity


【解决方案1】:

问题是我的处理程序实际上是在单独的线程上执行的,而不是在预期的 UI 线程上执行,因为处理程序在创建它们的线程上运行。我没有调用Looper.prepare()Looper.loop(),而是简单地指定处理程序将主线程的looper 与getMainLooper() 一起使用:

    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        public void run() {
            Intent intent = new Intent(context, EndGameActivity.class);
            context.startActivity(intent);
            GameActivity.activity.finish();
        }
    });

我还将呼叫转移到finish(),以防止活动在新活动创建之前完成。

【讨论】:

    猜你喜欢
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多