Android Load Cache Only无效。脱机显示WebView

时间:2011-11-04 14:49:01

标签: android caching webview android-webview offline-caching

我试图通过webview显示一些网页,而无需连接到互联网。 如果没有连接到互联网,我会收到Null-Pointer错误。如果有连接它正在工作。所以我认为这是

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 ComponentInfo{de.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

1 个答案:

答案 0 :(得分:2)

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

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

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

应该解决你的问题。