Android Webview NullPointerException
当我遇到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 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)加载xml布局webview3.xml可能缺少代码>
即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 linewebView.getSettings().setJavaScriptEnabled(true)代码>在哪里可以得到空指针异常?在哪一行可以得到NPEon行webView.getSettings().setJavaScriptEnabled(true)代码>