【问题标题】:Loading a webpage into an Android App将网页加载到 Android 应用程序中
【发布时间】:2015-01-20 00:32:50
【问题描述】:

我正在开发一个 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>

以下是我运行应用程序时发生的情况:

【问题讨论】:

    标签: java android android-activity webview


    【解决方案1】:

    哇,伙计们,很抱歉,如果这是垃圾邮件,但我刚刚找到了一个我以前没有看到的论坛解释。 Android 需要应用程序中的互联网权限才能访问互联网。我认为它会给我一个权限异常,但它只是不加载它。这是我添加到 xml 清单中以使其正常工作的内容

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

    再次抱歉,我不知道这件事真的很愚蠢。你会认为控制台会打印某种安全/权限异常

    【讨论】:

    • 是的,logcat输出有权限异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    相关资源
    最近更新 更多