webview崩溃NullPointerException android.webkit.WebViewDatabase.initDatabase(WebViewDatabase.java:234)

时间:2013-07-04 21:06:22

标签: android android-webview android-sqlite

我从谷歌播放消息中收到我的应用程序崩溃,在msg中

java.lang.NullPointerException
at android.webkit.WebViewDatabase.initDatabase(WebViewDatabase.java:234)
at android.webkit.WebViewDatabase.init(WebViewDatabase.java:212)
at android.webkit.WebViewDatabase.access$000(WebViewDatabase.java:40)
at android.webkit.WebViewDatabase$1.run(WebViewDatabase.java:193)

我没有在谷歌或stackoverflow中找到类似的问题,所以我不知道为什么这个痉挛,但我知道这是由webview引起的。

3 个答案:

答案 0 :(得分:1)

这里看起来与此问题相同(加上潜在的解决方法):

https://code.google.com/p/android/issues/detail?id=35204

我在这里找到了WebViewDatabase的代码(它的版本不完全相同,但是有足够的上下文来获取图片):

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/webkit/WebViewDatabase.java#WebViewDatabase.initDatabase%28android.content.Context%29

如果查看initDatabase()的代码,我在标有" ****"的行上有一个潜在的NPE。请注意,以下行检查NULL,因此看起来有点愚蠢:

 private void initDatabase(Context context) {
     try {
         mDatabase = context.openOrCreateDatabase(DATABASE_FILE, 0, null);
     } catch (SQLiteException e) {
         // try again by deleting the old db and create a new one
         if (context.deleteDatabase(DATABASE_FILE)) {
             mDatabase = context.openOrCreateDatabase(DATABASE_FILE, 0,
                     null);
         }
     }

     mDatabase.enableWriteAheadLogging(); ****

     // mDatabase should not be null,
     // the only case is RequestAPI test has problem to create db
     if (mDatabase == null) {
         mInitialized = true;
         notify();
         return;

答案 1 :(得分:0)

尝试检查调用android.webkit.WebViewDatabase.initDatabase()的任何代码 在某些情况下是否有错误处理或打开数据库的任何问题?

您是否有错误处理/对象检测?你能展示几行代码吗?

答案 2 :(得分:0)

我检查了提供的链接,android源代码,内部崩溃分析,似乎问题仅存在于android 4.0 - 4.0.4上。我已经在android 2.3.6,4.0.3,4.4.2上测试了我的建议,它似乎是正确的。所以我完成了以下解决方法:

package com.android.example;

import android.content.Context;
import android.os.Build;

public class WebViewUtil {

    private static final String WEBVIEW_DATABASE_FILE = "webview.db";

    public static boolean isWebViewCorrupted(Context context) {
        try {
            int currentSdk = Build.VERSION.SDK_INT;
            if (currentSdk == Build.VERSION_CODES.ICE_CREAM_SANDWICH
                    || currentSdk == Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                try {
                    context.openOrCreateDatabase(WEBVIEW_DATABASE_FILE, 0, null);
                } catch (Throwable t) {
                    // try again by deleting the old db and create a new one
                    context.deleteDatabase(WEBVIEW_DATABASE_FILE);
                    context.openOrCreateDatabase(WEBVIEW_DATABASE_FILE, 0, null);
                }
            }
            return false;
        } catch (Throwable t) {
        }
        return true;
    }
}