Android Load Cache Only is not working. Show WebVi

2019-03-02 08:09发布

I'm trying to show some webpages via webview without connecting to the internet. I'm getting a Null-Pointer Error if there is no connection to the internet. If there is a connection it is working. So I think it's something with

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

Please help me. I already checked if the AppCachePath is correct with getCacheDir(); Thanks a lot

        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条回答
闹够了就滚
2楼-- · 2019-03-02 08:38

One of two things is happening. Either cm is null or cm.getActiveNetworkInfo() is returning null. I'm guessing it's cm.getActiveNetworkInfo(). If I'm right, then changing

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

to

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

should fix your problem.

查看更多
登录 后发表回答