如何处理未捕获的异常?

时间:2015-08-29 09:35:53

标签: android android-webview stack-trace

我真的无法调试此问题。即使破坏的代码被封装到通用的try-catch中,该应用程序我只会崩溃。

这是"堆栈跟踪":

08-29 11:27:22.899    6542-6542/my.package W/dalvikvm﹕ VFY: 
unable to resolve virtual method 18442: Landroid/webkit/CookieManager;.removeAllCookies (Landroid/webkit/ValueCallback;)V
08-29 11:27:23.249    6542-6545/my.package D/dalvikvm﹕ GC_CONCURRENT freed 186K, 7% free 6486K/6919K, paused 23ms+17ms, total 105ms
08-29 11:27:23.499    6542-6542/my.package W/dalvikvm﹕ VFY: unable to resolve static method 17990: Landroid/view/ViewAnimationUtils;.createCircularReveal (Landroid/view/View;IIFF)Landroid/animation/Animator;
08-29 11:27:23.499    6542-6542/my.package W/dalvikvm﹕ VFY: unable to resolve static method 17990: Landroid/view/ViewAnimationUtils;.createCircularReveal (Landroid/view/View;IIFF)Landroid/animation/Animator;
08-29 11:27:23.649    6542-6542/my.package W/dalvikvm﹕ VFY: unable to resolve virtual method 18446: Landroid/webkit/CookieManager;.setAcceptThirdPartyCookies (Landroid/webkit/WebView;Z)V
08-29 11:27:24.299    6542-6542/my.package W/dalvikvm﹕ JNI WARNING: JNI method called with exception pending
08-29 11:27:24.299    6542-6542/my.package W/dalvikvm﹕ in Landroid/webkit/CookieManagerClassic;.nativeRemoveAllCookie:()V (GetStringUTFChars)
08-29 11:27:24.299    6542-6542/my.package W/dalvikvm﹕ Pending exception is:
08-29 11:27:24.299    6542-6542/my.package I/dalvikvm﹕ "main" prio=5 tid=1 NATIVE
08-29 11:27:24.299    6542-6542/my.package I/dalvikvm﹕ | group="main" sCount=0 dsCount=0 obj=0x40f616a0 self=0x40089138
08-29 11:27:24.299    6542-6542/my.package I/dalvikvm﹕ | sysTid=6542 nice=-19 sched=0/0 cgrp=apps handle=1074933552
08-29 11:27:24.309    6542-6542/my.package I/dalvikvm﹕ | schedstat=( 0 0 0 ) utm=87 stm=8 core=0
08-29 11:27:24.439    6542-6542/my.package I/dalvikvm﹕ #00  pc 00001260  /system/lib/libcorkscrew.so (unwind_backtrace_thread+27)
08-29 11:27:24.439    6542-6542/my.package I/dalvikvm﹕ #01  pc 0005f650  /system/lib/libdvm.so (dvmDumpNativeStack(DebugOutputTarget const*, int)+35)
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #02  pc 00053dde  /system/lib/libdvm.so (dvmDumpThreadEx(DebugOutputTarget const*, Thread*, bool)+289)
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #03  pc 00053e6e  /system/lib/libdvm.so (dvmDumpThread(Thread*, bool)+25)
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #04  pc 0003a216  /system/lib/libdvm.so
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #05  pc 0003d6b8  /system/lib/libdvm.so
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #06  pc 0024f6f2  /system/lib/libwebcore.so
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #07  pc 00244a48  /system/lib/libwebcore.so
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #08  pc 00244b24  /system/lib/libwebcore.so
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #09  pc 0024a42c  /system/lib/libwebcore.so
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #10  pc 0001f4b0  /system/lib/libdvm.so (dvmPlatformInvoke+112)
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #11  pc 0004dfc0  /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+355)
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ #12  pc 00028964  /system/lib/libdvm.so
08-29 11:27:24.449    6542-6542/my.package I/dalvikvm﹕ at android.webkit.CookieManagerClassic.nativeRemoveAllCookie(Native Method)
08-29 11:27:24.459    6542-6542/my.package I/dalvikvm﹕ at android.webkit.CookieManagerClassic.removeAllCookie(CookieManagerClassic.java:139)
08-29 11:27:24.459    6542-6542/my.package I/dalvikvm﹕ at w.application.MyApplication.doLogout(MyApplication.java:163)

如您所见,问题是由于已弃用的方法removeAllCookies。尽管如此,将所有设备定位到GingerBread,我需要该方法。我的代码是:

try {
    android.webkit.CookieManager webViewCookieManager = android.webkit.CookieManager.getInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        webViewCookieManager.removeAllCookies(null);
        webViewCookieManager.removeSessionCookies(null);
    } else {
        webViewCookieManager.removeAllCookie(); // When it tries to execute this line, it crashes.
        webViewCookieManager.removeSessionCookie();
    }
} catch (Exception e) {
    e.printStackTrace();
}

try-catch无法处理异常!该应用程序崩溃了。我无法理解为什么或如何处理它。

1 个答案:

答案 0 :(得分:2)

我只是缺少CookieSyncManager

由于removeAllCookies在不同的线程上运行,因此从未抛出异常。

要解决:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        webViewCookieManager.flush();
        webViewCookieManager.removeAllCookies(null);
        webViewCookieManager.removeSessionCookies(null);
} else {
        CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(this);
        cookieSyncMngr.startSync();
        webViewCookieManager.removeAllCookie();
        webViewCookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
}