【问题标题】:WebPage loading on browser instead of Webview in Activity [duplicate]在浏览器上加载网页而不是在 Activity 中加载 Webview [重复]
【发布时间】:2013-12-10 05:39:46
【问题描述】:

在我的 xml 文件中,我有文本,在该文本下方,我放置了一个 webview。 我正在尝试在 Webview 中打开 google 的主页。不是在 webview 中打开,而是在浏览器上打开网页。我想要的是网页应该在 webview 中加载,它位于一些文本下方。下面是我的代码:

           <TextView
                  android:id="@+id/txt"

                  android:text="Hello Android"
                  android:textSize="30sp"
                  android:textStyle="bold"
                  android:textColor="#003399"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:layout_centerHorizontal="true"
                  />

      <WebView 

                 android:id="@+id/webview"
                 android:layout_marginTop="50dp"
                 android:layout_below="@id/txt"
                 android:layout_height="fill_parent"
                 android:layout_width="fill_parent"/>

请帮助解决这个问题。谢谢

【问题讨论】:

标签: android android-webview


【解决方案1】:

创建您的活动,然后使用此代码...

mWebview = (WebView) findViewById(R.id.webview); mWebview.getSettings().setJavaScriptEnabled(true); // 启用javascript

      mWebview.setWebViewClient(new WebViewClient() {
          public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
              Toast.makeText(youractivity_name.this, description, Toast.LENGTH_SHORT).show();
          }
      });

      mWebview.loadUrl("http://www.yahoo.com");

【讨论】:

    【解决方案2】:

    这是因为结合了两件事:

    1. WebViewClient 未设置(设置为 null),这使得 WebView 尝试将每个导航作为意图提供给系统。由于您安装了浏览器,系统将尝试在那里处理该导航。
    2. 访问 google.com 通常会导致重定向,这就是前一点中有关导航的内容很重要的原因。

    试试这个:

    webview = (WebView) findViewById(R.id.webview);
    webview.setWebViewClient(new WebViewClient());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2013-10-12
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多