Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Webview NullPointerException_Android_Webview - Fatal编程技术网

Android Webview NullPointerException

Android Webview NullPointerException,android,webview,Android,Webview,当我遇到Webview的NullPointerException时,可能出现什么错误: WebView webView; webView = (WebView) findViewById(R.id.webView3); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("..."); 这是我的webview3.xml: <?xml version="1.0" encoding="utf-8"?> &l

当我遇到Webview的NullPointerException时,可能出现什么错误:

WebView webView;
webView = (WebView) findViewById(R.id.webView3);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("...");
这是我的webview3.xml:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>


NPE在线
webView.getSettings().setJavaScriptEnabled(true)


即setContentView(R.layout.webview3.xml)

加载xml布局webview3.xml时可能缺少 即setContentView(R.layout.webview3.xml)

试试这个示例代码

res/layout/webview3.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
           android:orientation="vertical" 
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent"> 

      <WebView
              android:id="@+id/webview" 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"/>

</LinearLayout> 
试试这个示例代码

res/layout/webview3.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
           android:orientation="vertical" 
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent"> 

      <WebView
              android:id="@+id/webview" 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"/>

</LinearLayout> 
另一种方式:

    View webViewLayout = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.web_main, null, false);
    WebView webView = (WebView) webViewLayout.findViewById(R.id.webview);
另一种方式:

    View webViewLayout = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.web_main, null, false);
    WebView webView = (WebView) webViewLayout.findViewById(R.id.webview);

在哪一行可以得到NPEon line
webView.getSettings().setJavaScriptEnabled(true)在哪里可以得到空指针异常?在哪一行可以得到NPEon行
webView.getSettings().setJavaScriptEnabled(true)