WebView return 空指针异常

WebView return null pointer exception

我在 android 中遇到 webview 问题。 这是 class 网络视图的代码:

public class WebViewsActivity extends Activity {

    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_views);
        Intent thisIntent = getIntent();
        final String link = thisIntent.getExtras().getString("link");
        Log.e("link is :",link);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(link);
    }
}

这是 Exception :

04-27 01:17:44.669 25210-25210/customlistviewvolley.androidhive.info.customlistviewvolley E/OpenGLRenderer﹕ GL_INVALID_OPERATION 04-27 01:17:46.889 25210-25210/customlistviewvolley.androidhive.info.customlistviewvolley E/link is :﹕ http://www.lequipe.fr/Football/Actualites/Alexandre-lacazette-je-ne-compte-pas-m-arreter-la/554284#xtor=RSS-1 04-27 01:17:46.929 25210-25210/customlistviewvolley.androidhive.info.customlistviewvolley E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{customlistviewvolley.androidhive.info.customlistviewvolley/info.androidhive.customlistviewvolley.WebViewsActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) at android.app.ActivityThread.access0(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5473) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at info.androidhive.customlistviewvolley.WebViewsActivity.onCreate(WebViewsActivity.java:24) at android.app.Activity.performCreate(Activity.java:5234) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)             at android.app.ActivityThread.access0(ActivityThread.java:148)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)             at android.os.Handler.dispatchMessage(Handler.java:99)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:5473)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:525)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)             at dalvik.system.NativeStart.main(Native Method)

第 24 行是:webView.getSettings().setJavaScriptEnabled(true); 清单中添加了互联网权限。

您缺少以下行以获取对 WebView 实例的引用

WebView webView = (WebView)findViewById(R.id.your_webview_id);

应该在

之前添加
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(link);

您可以在下面的行

之前添加此行 webView = new WebView(this); 以获取 webview 的引用
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(link);

希望这会有所帮助。

您没有实例化 webView 字段。这由日志中的这一行指示:

dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at info.androidhive.customlistviewvolley.WebViewsActivity.onCreate(WebViewsActivity.java:24)