我有三项游戏活动。第一个是菜单,第二个是游戏活动,最后一个是游戏结束页面,显示玩家的分数。当我进入最后一个活动时,按后退按钮会使应用程序无响应。
我在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
启动新级别,因此可以关闭它。计时器用于产生和渲染新的敌人,所以它也应该被取消。
线程在相应的surfaceDestroyed
中通过SurfaceView
停止。
在我的最后一项活动中,我使用endGame
提取SharedPreferences
方法中的两个值并将其设为TextView
s:
@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)
答案 0 :(得分:0)
问题是我的处理程序实际上是在一个单独的线程而不是预期的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()
,以防止活动在创建新活动之前完成。