如何在android应用程序中将网页加载到webview?

如何在android应用程序中将网页加载到webview?,android,android-layout,webview,Android,Android Layout,Webview,我正在尝试制作一个简单的android应用程序,将网页加载到WebView package com.example.abhi.molinahealthcare; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebSettings; im

我正在尝试制作一个简单的android应用程序,将网页加载到
WebView

package com.example.abhi.molinahealthcare;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Molina_HealthCare extends ActionBarActivity {
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    WebView webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_molina__health_care);
        WebView myWebView = (WebView) findViewById(R.id.webview);

    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new MyWebViewClient());
    openURL();
}

private void openURL() {
    webview.loadUrl("http://www.google.com");
    webview.requestFocus();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_molina__health_care, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}
活动:-

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



我没能打开它。我拥有AndroidManifest.xml中声明的
INTERNET
权限

您没有初始化在
openURL
方法中使用的webview的
webview
对象。通过在
onCreate
中初始化
webview
而不是
myWebView
来执行此操作:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_molina__health_care);
        webview = (WebView) findViewById(R.id.webview);
        ...

您是否在
AndroidManifest.xml
中添加了
互联网
权限?是的,我也添加了互联网权限,我只想在我的android应用程序上加载网页,但不使用android浏览器!这样的错误试图解决问题的人通常不会注意到,有一段时间我觉得他的代码是正确的