WebView返回空指针异常

时间:2015-04-27 00:26:29

标签: android nullpointerexception android-webview

我在android中遇到了webview的问题。 这是webview类的代码:

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是::     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:致命异常:主要         java.lang.RuntimeException:无法启动活动ComponentInfo {customlistviewvolley.androidhive.info.customlistviewvolley / info.androidhive.customlistviewvolley.WebViewsActivity}:     显示java.lang.NullPointerException                 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)                 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)                 在android.app.ActivityThread.access $ 900(ActivityThread.java:148)                 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1319)                 在android.os.Handler.dispatchMessage(Handler.java:99)                 在android.os.Looper.loop(Looper.java:137)                 在android.app.ActivityThread.main(ActivityThread.java:5473)                 at java.lang.reflect.Method.invokeNative(Native Method)                 在java.lang.reflect.Method.invoke(Method.java:525)                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:854)                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)                 at dalvik.system.NativeStart.main(Native Method)          引起:java.lang.NullPointerException                 在info.androidhive.customlistviewvolley.WebViewsActivity.onCreate(WebViewsActivity.java:24)                 在android.app.Activity.performCreate(Activity.java:5234)                 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)                 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)     在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)     在android.app.ActivityThread.access $ 900(ActivityThread.java:148)     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1319)     在android.os.Handler.dispatchMessage(Handler.java:99)     在android.os.Looper.loop(Looper.java:137)     在android.app.ActivityThread.main(ActivityThread.java:5473)     at java.lang.reflect.Method.invokeNative(Native Method)     在java.lang.reflect.Method.invoke(Method.java:525)     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:854)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)     在dalvik.system.NativeStart.main(本地方法)

  

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

3 个答案:

答案 0 :(得分:2)

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

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

应该在

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

答案 1 :(得分:1)

您可以在行

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

希望这会有所帮助。

答案 2 :(得分:1)

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

  

dalvik.system.NativeStart.main(Native Method)由以下引发:info.androidhive.customlistviewvolley.WebViewsActivity.onCreate(WebViewsActivity.java:24)中的java.lang.NullPointerException