When ever I try to use a web view it gives me a null pointer exception, my code:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class Main extends Activity {
WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
开发者_JAVA百科 setContentView(R.layout.main);
webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/www/index.html");
}
}
the Logs are:
02-18 22:08:06.224: ERROR/AndroidRuntime(355): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.web/com.test.web.Main}: java.lang.NullPointerException 02-18 22:08:06.224: ERROR/AndroidRuntime(355): Caused by: java.lang.NullPointerException 02-18 22:08:06.224: ERROR/AndroidRuntime(355): at com.test.web.Main.onCreate(Main.java:17)
(there where more logs just none of them pertained to the problem)
On a semi- related note how do you make code look like code on stackexchange?
So I'm guessing the webView isn't initializing correctly?
You're getting NullPointerException because in your main.xml file you have defined a TextView not a WebView.
Just change the TextView to WebView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<WebView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView" />
</LinearLayout>
精彩评论