我开发了一个来自android stuido的简单webview应用程序,它将在线时加载页面的url并在离线时显示缓存文件。但是我得到了
的常数错误print "".join(typed_num)
以下是我的Mainactivity .java代码
Null pointer Exception
at java.lang.reflect.Method.invoke(Native Method)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit
.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
并从我的manifest.xml文件
package com.example.sagar.cdproutine;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = null;
WebView webView = new WebView(context);
webView.getSettings().setAppCacheMaxSize(5 * 1024 * 1024); // 5MB
webView.getSettings().setAppCachePath(
getApplicationContext().getCacheDir().getAbsolutePath());
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // load online by default
if (!isNetworkAvailable()) { // loading offline
webView.getSettings().setCacheMode(
WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
webView.loadUrl("http://www.sagarrawal.com.np");
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo =
connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
}
我认为Context导致问题,但我不知道如何解决它。 任何帮助都会受到很大的赞赏。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sagar.cdproutine">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
答案 0 :(得分:0)
我错过了什么吗? 此处的此行为空,您将其传递给Web视图。
Context context = null;
尝试这样的事情:
WebView webView = new WebView(getApplicationContext());
答案 1 :(得分:0)
正如David所提到的那样,不传递null Context
这是在OnCreate
webView = (WebView) findViewById(R.id.webView);
initWebView();//Initialize the WebView
webView.loadUrl(url);//pass your Url
对于initWebView();
private void initWebView() {
webView.setWebChromeClient(new MyWebChromeClient(this));
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webView.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
});
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
webView.setHorizontalScrollBarEnabled(false);
}
对于MyWebChromeClient,请执行以下操作:
private class MyWebChromeClient extends WebChromeClient {
Context context;
public MyWebChromeClient(Context context) {
super();
this.context = context;
}
}
另请参阅此引用 - link