简体   繁体   中英

How to use webview in Android xml?

I have created layout.xml.

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

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

    </LinearLayout>

This is in my Activity. I m getting this Web-view can not be resolved or is not a field and getting nullpointerexception.Here is my code.

     @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);
                mWebview=(WebView)findViewById(R.id.mwebView);

                new getContactsTask().execute();

            }

Change your Java code:

setContentView(R.layout.layout) //true

Instead of:

setContentView(R.layout.main);  //false
                   WebView webView=(WebView) findViewById(R.id.mwebView);
                   webView.getSettings().setBuiltInZoomControls(true);
                   WebSettings webSettings = webView.getSettings();
                   webSettings.setJavaScriptEnabled(true);
                   webSettings.setAllowFileAccess(true);
                   String htmlCode="";
                   webView.loadDataWithBaseURL("", htmlCode, "text/html","utf-8", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM