Android 仅加载缓存不起作用。离线显示 WebView

Posted

技术标签:

【中文标题】Android 仅加载缓存不起作用。离线显示 WebView【英文标题】:Android Load Cache Only is not working. Show WebView offline 【发布时间】:2011-12-22 02:28:26 【问题描述】:

我正在尝试通过 webview 显示一些网页而不连接到互联网。 如果没有连接到互联网,我会收到空​​指针错误。如果有连接,它正在工作。所以我认为这是与

webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
webView.loadUrl("http://www.google.de");

请帮助我。我已经用 getCacheDir() 检查了 AppCachePath 是否正确; 非常感谢

        webView.getSettings().setjavascriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAppCacheMaxSize(1024*1024*8);
        webView.getSettings().setAppCachePath("/data/data/de.app/cache");
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAppCacheEnabled(true);


        webView.setWebChromeClient(new WebChromeClient() 
              @Override
              public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,
                           WebStorage.QuotaUpdater quotaUpdater)
              
                    quotaUpdater.updateQuota(spaceNeeded * 2);
              
        );





ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        if(cm.getActiveNetworkInfo().isConnected()== true)
        
            webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            webView.loadUrl("http://www.google.de");
        
        else
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
            webView.loadUrl("http://www.google.de");

        

11-05 12:46:20.213: WARN/dalvikvm(15681): threadid=3: thread exiting with uncaught exception (group=0x4001b180)
11-05 12:46:20.213: ERROR/androidRuntime(15681): Uncaught handler: thread main exiting due to uncaught exception
11-05 12:46:20.323: ERROR/AndroidRuntime(15681): java.lang.RuntimeException: Unable to start activity ComponentInfode.webstest/de.webstest.webbrowser: java.lang.NullPointerException
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at android.os.Looper.loop(Looper.java:123)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at android.app.ActivityThread.main(ActivityThread.java:4363)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at java.lang.reflect.Method.invoke(Method.java:521)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at dalvik.system.NativeStart.main(Native Method)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681): Caused by: java.lang.NullPointerException
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at de.webstest.webbrowser.onCreate(webbrowser.java:87)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-05 12:46:20.323: ERROR/AndroidRuntime(15681):     ... 11 more

【问题讨论】:

欢迎来到 ***!如果您发现回复有帮助,请投票。如果回复成功回答了您的问题,请点击旁边的绿色复选标记接受答案。也请查看***.com/questions/how-to-ask 以获取有关如何写出好问题的建议 你得到什么空指针异常?它在 logcat 中说了什么? 嗨,你真好,你帮了我。所以这里是日志:WARN/dalvikvm(15681): threadid=3: thread exiting with uncaught exception (group=0x4001b180) ERROR/AndroidRuntime(15681): Uncaught handler: thread main exiting due to unaught exception ERROR/AndroidRuntime(15681) : java.lang.RuntimeException: 无法启动活动 ComponentInfode.webstest/de.webstest.webbrowser: java.lang.NullPointerException ERROR/AndroidRuntime(15681): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496 ) 代码中的第 87 行在哪里? if(cm.getActiveNetworkInfo().isConnected()== true) 【参考方案1】:

正在发生两件事之一。 cm 为 null 或 cm.getActiveNetworkInfo() 返回 null。我猜是cm.getActiveNetworkInfo()。如果我是对的,那就改变

if(cm.getActiveNetworkInfo().isConnected()== true)

if(cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected() == true)

应该可以解决您的问题。

【讨论】:

以上是关于Android 仅加载缓存不起作用。离线显示 WebView的主要内容,如果未能解决你的问题,请参考以下文章

Android WebView 未从缓存中加载第二页

Android Webview离线时加载缓存

android上的javascript地理定位在离线模式下不起作用

缓存在 UIWebView 中不起作用

缓存在UIWebView中不起作用

片段隐藏在Android中不起作用