我正在开发一个 Android 应用程序,我希望能够在应用程序中加载网页。我在互联网上浏览了一段时间,似乎无法得到正确的结果。我知道我的互联网连接已建立,因为我可以使用浏览器。我对 Android 开发和学习很陌生,所以任何详细说明都会令人惊叹。这是我正在使用的代码:
public class MainActivity extends Activity {
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Button btnBack;
WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(WebView)findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
/** Opens the URL in a browser */
private void openURL() {
webview.loadUrl("http://www.google.com");
webview.requestFocus();
}
}
和我的 xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_below="@+id/rlayout1"
android:id="@+id/rlayout2">
<WebView android:id="@+id/webview1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</RelativeLayout>
以下是我运行应用程序时发生的情况:
最佳答案
哇,伙计们,抱歉,如果这是垃圾邮件,但我刚刚找到了一个我以前没有看到的论坛解释。 Android 需要应用程序具有互联网权限才能访问互联网。我以为它会给我一个权限异常,但它只是不加载它。这是我添加到 xml list 中以使其正常工作的内容
<uses-permission android:name="android.permission.INTERNET" />
再次抱歉,我因为不知道这一点而感到非常愚蠢。您可能认为控制台会打印某种安全/权限异常
关于java - 将网页加载到 Android 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28035923/