四. 调试程序
好了,现在你已经写好了你的应用程序,把它放到模拟器里跑一下看看效果吧。在Eclipse中,点击Run->Run菜单,Eclipse会自动编译安装你的应用到手机/模拟器中。应用编译成功后会生成.apk文件,你也可以用adb install手工安装你的apk文件。如果程序出现force close怎么办?下图展示了一个应用force close时的屏幕截图。

这时你需要使用adb logcat查看系统log。有经验的程序员可以在log中发现很多有用信息。在嵌入式系统中,很多时候没办法调试程序,读log分析问题才是王道。这里我给出一个程序force close时的log。注意看"Shutting down VM"前面的那句话,这个应用需要读取Android market相关的信息,但平台中没有安装Android market,这是导致应用force close的真正原因。
D/dalvikvm( 844): GC freed 13377 objects / 604304 bytes in 248ms
I/ActivityManager( 844): Starting activity: Intent { action=android.intent.action.VIEW data=market://search?q=pname:com.android.wallswitch1 }
D/AndroidRuntime( 3162): Shutting down VM
W/dalvikvm( 3162): threadid=3: thread exiting with uncaught exception (group=0x40010e28)
E/AndroidRuntime( 3162): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 3162): android.content.ActivityNotFoundException: No Activity found to handle Intent { action=android.intent.action.VIEW data=market://search?q=pname:com.android.wallswitch1 }
E/AndroidRuntime( 3162): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1472)
E/AndroidRuntime( 3162): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1442)
E/AndroidRuntime( 3162): at android.app.Activity.startActivityForResult(Activity.java:2597)
E/AndroidRuntime( 3162): at android.app.Activity.startActivity(Activity.java:2641)
E/AndroidRuntime( 3162): at com.android.wallswitch1.wallswitch1$5.onClick(wallswitch1.java:238)
E/AndroidRuntime( 3162): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:153)
E/AndroidRuntime( 3162): at android.os.Handler.dispatchMessage(Handler.java:88)
E/AndroidRuntime( 3162): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 3162): at android.app.ActivityThread.main(ActivityThread.java:3742)
E/AndroidRuntime( 3162): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3162): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime( 3162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
E/AndroidRuntime( 3162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
E/AndroidRuntime( 3162): at dalvik.system.NativeStart.main(Native Method)
I/Process ( 844): Sending signal. PID: 3162 SIG: 3
I/dalvikvm( 3162): threadid=7: reacting to signal 3
D/ActivityManager( 844): ###### [ap_panic_report] = off
I/dalvikvm( 3162): Wrote stack trace to '/local/log/traces.txt'
I/ActivityManager( 844): Starting activity: Intent { action=android.intent.action.VIEW data=market://search?q=pname:com.android.wallswitch1 }
D/AndroidRuntime( 3162): Shutting down VM
W/dalvikvm( 3162): threadid=3: thread exiting with uncaught exception (group=0x40010e28)
E/AndroidRuntime( 3162): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 3162): android.content.ActivityNotFoundException: No Activity found to handle Intent { action=android.intent.action.VIEW data=market://search?q=pname:com.android.wallswitch1 }
E/AndroidRuntime( 3162): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1472)
E/AndroidRuntime( 3162): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1442)
E/AndroidRuntime( 3162): at android.app.Activity.startActivityForResult(Activity.java:2597)
E/AndroidRuntime( 3162): at android.app.Activity.startActivity(Activity.java:2641)
E/AndroidRuntime( 3162): at com.android.wallswitch1.wallswitch1$5.onClick(wallswitch1.java:238)
E/AndroidRuntime( 3162): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:153)
E/AndroidRuntime( 3162): at android.os.Handler.dispatchMessage(Handler.java:88)
E/AndroidRuntime( 3162): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 3162): at android.app.ActivityThread.main(ActivityThread.java:3742)
E/AndroidRuntime( 3162): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3162): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime( 3162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
E/AndroidRuntime( 3162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
E/AndroidRuntime( 3162): at dalvik.system.NativeStart.main(Native Method)
I/Process ( 844): Sending signal. PID: 3162 SIG: 3
I/dalvikvm( 3162): threadid=7: reacting to signal 3
D/ActivityManager( 844): ###### [ap_panic_report] = off
I/dalvikvm( 3162): Wrote stack trace to '/local/log/traces.txt'
再来举一个例子,你能从下面的log中看出这个应用force close的原因吗?
I/ActivityManager( 855): Starting activity: Intent { action=android.intent.action.MAIN flags=0x10000000 comp={com.iflytek.aisound.demo/com.iflytek.aisound.demo.AisoundDemo} }
D/InputMethodManager( 923): static exitContext
D/mountd ( 640): USB_DETECT: msg is 'add@/kernel/uids/10067'
I/ActivityManager( 855): Start proc com.iflytek.aisound.demo for activity com.iflytek.aisound.demo/.AisoundDemo: pid=1162 uid=10067 gids={}
I/Config ( 855): 22222 Input configuration changed: null
D/Focus ( 923): Lose Window focus: "" com.db4o.servo.search.SearchBar$3@43820f78
D/DcdHomeLayout( 923): setDisplayedChild: 1
D/DcdHomeLayout( 923): scroll to (0, 0), this=com.android.borqshome.dcd.DcdHomeLayout$EntryTextLayout@43825be8
D/DcdHomeLayout( 923): height=74 m_scrollingShift=64
D/dalvikvm( 1162): Trying to load lib /sdcard/iflytek/libAisound4.so 0x43876e40
E/MediaPlayer( 1162): constructor
E/MediaPlayer( 1162): setListener
D/dalvikvm( 1162): Added shared lib /sdcard/iflytek/libAisound4.so 0x43876e40
W/dalvikvm( 1162): ERROR: Unable to find decl for native Lcom/iflytek/aisound/Data;.JNICALL Java_com_iflytek_aisound_Data_prepareData (Ljava/lang/String;I)V
D/dalvikvm( 1162): +++ not scanning '/system/lib/libwebcore.so' for 'ContrlInit' (wrong CL)
D/dalvikvm( 1162): +++ not scanning '/system/lib/libmedia_jni.so' for 'ContrlInit' (wrong CL)
D/Focus ( 1162): Gain Window focus: "" android.widget.EditText@43880570
I/DEBUG ( 641): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG ( 641): Build fingerprint: 'generic/OMS1_0/OMS1_0/:1.0/ohd-stable-114235/eng..20090115.111116:eng/test-keys'
I/DEBUG ( 641): pid: 1162, tid: 1169 >>> com.iflytek.aisound.demo <<<
I/DEBUG ( 641): cannot get registers: No such process
I/DEBUG ( 641): signal 11 (SIGSEGV), fault addr 0000000c
I/DEBUG ( 641): r0 00000000 r1 44c6dd34 r2 00000001 r3 00000024
I/DEBUG ( 641): r4 00000000 r5 00000024 r6 00000001 r7 41048fd8
I/DEBUG ( 641): r8 44c6dd70 r9 41048fd0 10 41048fc0 fp 44c6dd6c
I/DEBUG ( 641): ip 805e3678 sp 44c6dd00 lr afe12583 pc afe122e4 cpsr 20000030
I/DEBUG ( 641): #00 pc afe122e4 /system/lib/libc.so
I/DEBUG ( 641): #01 pc afe12580 /system/lib/libc.so
I/DEBUG ( 641): stack:
D/InputMethodManager( 923): static exitContext
D/mountd ( 640): USB_DETECT: msg is 'add@/kernel/uids/10067'
I/ActivityManager( 855): Start proc com.iflytek.aisound.demo for activity com.iflytek.aisound.demo/.AisoundDemo: pid=1162 uid=10067 gids={}
I/Config ( 855): 22222 Input configuration changed: null
D/Focus ( 923): Lose Window focus: "" com.db4o.servo.search.SearchBar$3@43820f78
D/DcdHomeLayout( 923): setDisplayedChild: 1
D/DcdHomeLayout( 923): scroll to (0, 0), this=com.android.borqshome.dcd.DcdHomeLayout$EntryTextLayout@43825be8
D/DcdHomeLayout( 923): height=74 m_scrollingShift=64
D/dalvikvm( 1162): Trying to load lib /sdcard/iflytek/libAisound4.so 0x43876e40
E/MediaPlayer( 1162): constructor
E/MediaPlayer( 1162): setListener
D/dalvikvm( 1162): Added shared lib /sdcard/iflytek/libAisound4.so 0x43876e40
W/dalvikvm( 1162): ERROR: Unable to find decl for native Lcom/iflytek/aisound/Data;.JNICALL Java_com_iflytek_aisound_Data_prepareData (Ljava/lang/String;I)V
D/dalvikvm( 1162): +++ not scanning '/system/lib/libwebcore.so' for 'ContrlInit' (wrong CL)
D/dalvikvm( 1162): +++ not scanning '/system/lib/libmedia_jni.so' for 'ContrlInit' (wrong CL)
D/Focus ( 1162): Gain Window focus: "" android.widget.EditText@43880570
I/DEBUG ( 641): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG ( 641): Build fingerprint: 'generic/OMS1_0/OMS1_0/:1.0/ohd-stable-114235/eng..20090115.111116:eng/test-keys'
I/DEBUG ( 641): pid: 1162, tid: 1169 >>> com.iflytek.aisound.demo <<<
I/DEBUG ( 641): cannot get registers: No such process
I/DEBUG ( 641): signal 11 (SIGSEGV), fault addr 0000000c
I/DEBUG ( 641): r0 00000000 r1 44c6dd34 r2 00000001 r3 00000024
I/DEBUG ( 641): r4 00000000 r5 00000024 r6 00000001 r7 41048fd8
I/DEBUG ( 641): r8 44c6dd70 r9 41048fd0 10 41048fc0 fp 44c6dd6c
I/DEBUG ( 641): ip 805e3678 sp 44c6dd00 lr afe12583 pc afe122e4 cpsr 20000030
I/DEBUG ( 641): #00 pc afe122e4 /system/lib/libc.so
I/DEBUG ( 641): #01 pc afe12580 /system/lib/libc.so
I/DEBUG ( 641): stack: