简体   繁体   中英

Can't see WebView

I want to programmatically add WebView to my Activity . This is my code:

public class MainActivity extends Activity {

    private WebView wv;

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

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        wv = new WebView(this);
        wv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        linearLayout.addView(wv);

        super.setContentView(linearLayout);
    }

    @Override
    protected void onResume() {
        super.onResume();
        wv.loadUrl("www.google.com");
    }
}

And this is AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testing.kaarelp.learn">

    <application>

        <uses-permission android:name="android.permission.INTERNET" />

        <activity android:name=".pakk.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

But when I open the app I get a blank white screen. Why isn't it showing www.google.com?

------------- edit 2 --------------

The problem was not putting https:// or ( http:// ) in front of www.google.com . So it should be:

wv.loadUrl("https://www.google.com");

Your whole code is fine. You just need to pass pass web link using http or https.

webview wv;
        @override
        protected void oncreate(bundle savedinstancestate) {
            super.oncreate(savedinstancestate);

            linearlayout linearlayout = new linearlayout(this);
            linearlayout.setorientation(linearlayout.vertical);

            wv = new webview(this);
            wv.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent));
            linearlayout.addview(wv);

            super.setcontentview(linearlayout);
        }

    @override
    protected void onresume() {
        super.onresume();
        wv.loadurl("http://www.google.com");
    }
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final WebView webView = (WebView) findViewById(R.id.fragment_about_us_wv);

            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("https://www.google.co.in/");
            return view;
        }

and xml like

<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/fragment_about_us_wv"
        android:layout_width="match_parent"
        android:background="@color/backGroundColor"
        android:layout_height="match_parent" />

</LinearLayout>

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