English 中文(简体)
网上浏览
原标题:Loading cache in offline mode in Android Webview

我发现,应当检查申请是否离线,必须装上海滩,或者在网上装货,必须装满。 这是我所用的法典:

ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
String weblink = "http://www.google.com";
if(cm.getActiveNetworkInfo().isConnected()){
    myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    myWebView.loadUrl(weblink);
} else {
    myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    myWebView.loadUrl(weblink);
}

这些是我允许的:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permisson.ACCESS_WIFI_STATE"/>

我的问题是: 当我身穿飞机或我的联系被切断时。 部队关闭。 当一米线上线时,照装和正常运行...... 我错了什么?

这是我的标志:

11-22 10:09:40.310: E/AndroidRuntime(323): FATAL EXCEPTION: main
11-22 10:09:40.310: E/AndroidRuntime(323): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.martijngijselaar.rooster/com.martijngijselaar.rooster.SaxionRoosterActivity}: java.lang.NullPointerException
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.os.Looper.loop(Looper.java:123)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-22 10:09:40.310: E/AndroidRuntime(323):  at java.lang.reflect.Method.invokeNative(Native Method)
11-22 10:09:40.310: E/AndroidRuntime(323):  at java.lang.reflect.Method.invoke(Method.java:521)
11-22 10:09:40.310: E/AndroidRuntime(323):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-22 10:09:40.310: E/AndroidRuntime(323):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-22 10:09:40.310: E/AndroidRuntime(323):  at dalvik.system.NativeStart.main(Native Method)
11-22 10:09:40.310: E/AndroidRuntime(323): Caused by: java.lang.NullPointerException
11-22 10:09:40.310: E/AndroidRuntime(323):  at com.martijngijselaar.rooster.SaxionRoosterActivity.onCreate(SaxionRoosterActivity.java:34)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 10:09:40.310: E/AndroidRuntime(323):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-22 10:09:40.310: E/AndroidRuntime(323):  ... 11 more

更便利于此点的是整个法典:

package com.martijngijselaar.rooster;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class SaxionRoosterActivity extends Activity {
    private static final int MENU1 = Menu.FIRST;
    private static final int MENU2 = Menu.FIRST + 1;
    private static final int MENU3 = Menu.FIRST + 2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());

        // Checken of er een connectie is, zo niet dan de chache laden, zo wel dan gewoon het netwerk laden!!
        ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
        String weblink = "http://google.com/";
        if(cm.getActiveNetworkInfo().isConnected()){
            myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
            myWebView.loadUrl(weblink);
        } else {
            myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            myWebView.loadUrl(weblink);
        }

        // Zorgen dat je input kunt gebruiken in de webview
        myWebView.requestFocus(View.FOCUS_DOWN);
        myWebView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_UP:
                        if (!v.hasFocus()) {
                            v.requestFocus();
                        }
                        break;
                }
                return false;
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU1, 0, "Wijzig klas");
        menu.add(0, MENU2, 0, "Disclaimer");
        menu.add(0, MENU3, 0, "Sluiten");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        WebView myWebView = (WebView) findViewById(R.id.webview);
        switch (item.getItemId()) {
        case MENU1:
            myWebView.loadUrl("http://google.nl");
            return true;
        case MENU2:
            myWebView.loadUrl("http://google.nl");
            return true;
        case MENU3:
            finish();
            return true;
        }
        return false;
    }
}
最佳回答

Your check for the connection needs to be tweaked. Although there is a property called isConnected you first need to check if getActiveNetworkInfo() returns null. If it returns null, there is no active network info - this is what you would get in airplane mode. This is the check that I use for network connectivity

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}
问题回答

暂无回答




相关问题
Write-though caching of large data sets in WCF?

We ve got a smart client that talks to a SQL Server database via WCF, displaying the entities in the database, and allowing the user to edit those entities. Some of the WCF calls return a large data ...

Clearing RSL in Cache

I have built a flex application which has a "main" project and it is assosciated with a few RSL s which are loaded and cached once i run my "main" application. The problem i am facing is that the ...

how to tell clicking "back" to load cache?

I would like for my site when someone clicks "Back" or "Forward" for the server to tell the browser to load the cache instead of reloading the entire page. I ve tested some headers and done research, ...

java plugin cache and dynamic IP host

I m trying to use Amazon S3 and Amazon Cloudfront CDN to deliver the jar files of my applet application. I m seeing several cache misses of my jars by the java plugin. This is a show-stopper for me, ...

Frequently Used metadata Hashmap

Are there any implementations of a static size hashtable that limits the entries to either the most recently or most frequently used metadata? I would prefer not to keep track of this information ...

PHP - Memcache - HTML Caching

I would like to create a caching system that will bypass some mechanisms in order to improve the performance. I have some examples: 1-) I have a dynamic PHP page that is updated every hour. The page ...

Performance of Sql subqueriesfunctions

I am currently working on a particularly complex use-case. Simplifying below :) First, a client record has a many-to-one relationship with a collection of services, that is, a single client may have ...

热门标签