打开新的Andr​​oid活动与web视图显示一个空白(白色)屏幕而不是HTML内容 [英] Opening new android activity with a webview shows a blank (white) screen instead of the html content

查看:114
本文介绍了打开新的Andr​​oid活动与web视图显示一个空白(白色)屏幕而不是HTML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究2天,我还没有发现类似的问题随时随地在互联网上!

从主要活动,我试图打开一个包含web视图,将在android_asset文件夹中显示一个HTML页面(about.html)一项新的活动。

我能够推出新的活动(test.java)与正常显示的web视图,但网页内容只是没有露面。即使是web视图的垂直滚动条的显示方式(如页面内容比较长),我可以滚动页面上下,但其中不带内容!

 包com.example.test;

进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.net.ConnectivityManager;
进口android.net.NetworkInfo;
进口android.net.Uri;
进口android.os.Bundle;
进口android.view.KeyEvent;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;
进口android.widget.TextView;

公共类测试延伸活动{
私人TextView的apppagetitle;
私人的WebView browser_rn;

@覆盖
公共无效的onCreate(包saveInstanceState){
    super.onCreate(saveInstanceState);

    的setContentView(R.layout.test);

    browser_rn =(web视图)findViewById(R.id.webkit);
    browser_rn.getSettings()setJavaScriptEnabled(真)。
    browser_rn.getSettings()setPluginsEnabled(真)。
    browser_rn.setScrollBarStyle(0);

    最后AlertDialog alertDialog =新AlertDialog.Builder(本).create();
    apppagetitle =(TextView中)findViewById(R.id.apppagetitle);


    browser_rn.setWebViewClient(新WebViewClient(){

        公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
            view.loadUrl(URL);
            返回false;
        }

        公共无效onPageFinished(web视图查看,字符串URL){
            apppagetitle.setText(browser_rn.getTitle());
        }

        公共无效onReceivedError(web视图来看,INT错误code,字符串描述,字符串failingUrl){
            如果(!isInternetOn()){
                alertDialog.setTitle(连接错误);
                alertDialog.setMessage(你需要连接到互联网。);
            } 其他 {
                alertDialog.setTitle(错误);
                alertDialog.setMessage(介绍);
            }
            alertDialog.setButton(OK,新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface对话,诠释它){
                    返回;
                }
            });
            alertDialog.show();
        }
    });

    browser_rn.loadUrl(文件:///android_asset/about.html);
}

公共布尔的onkeydown(INT键code,KeyEvent的事件){
    如果(键code == KeyEvent.KEY code_BACK和放大器;&安培; event.getRepeatCount()== 0){
        browser_rn.clearCache(真正的);
        browser_rn.clearHistory();
        this.finish();
        返回true;
    }
    返回super.onKeyDown(键code,事件);
}
}
 

奇怪的是,新的活动(test.java)可以顺利拿到,并在TextView中显示网页标题的时候我叫webview.gettitle(),但HTML内容只是没有呈现在web视图。我看到的是滚动条空白屏幕。我怀疑这是布尔shouldOverrideUrlLoading这可能会导致它。

更新:

感谢您的答复。

的test.xml

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:的myapp =htt​​p://schemas.android.com/apk/res/com.example.test
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的LinearLayout
  机器人:方向=横向
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =WRAP_CONTENT
  机器人:背景=@可绘制/绿
>
<的TextView
机器人:ID =@ + ID / apppagetitle
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=#FFFFFF
机器人:文本=奇新闻
机器人:TEXTSIZE =25像素
机器人:填充=10px的
/>
< / LinearLayout中>
<的LinearLayout
  机器人:方向=垂直
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT>
<的WebView
机器人:ID =@ + ID / WebKit的
机器人:layout_width =FILL_PARENT
机器人:layout_height =0px
机器人:layout_weight =1
/>
< / LinearLayout中>
< / LinearLayout中>
 

about.html 很简单,下面是什么。

 <!DOCTYPE HTML>
< HTML>
< HEAD>
<元字符集=utf-8/>
<标题>关于< /标题>
< /头>
<身体GT;
< H3风格=颜色:#0066dd>关于< / H3>

< P>此处的一些文字...< / P>

< /身体GT;
< / HTML>
 

哦,是的,我忘了前面提及的同一块code时,about.html有时会出现,如果我强行停止和重新启动我的手机上的应用程序。然而,它随后将不会出现在以后启动。在某些场合,当它成功地显示了about.html,我preSS后退按钮返回到主活动,它会再次消失,当我从主要活动重新启动该活动。我迷路了,但我有一个强烈的感觉是shouldOverrideUrlLoading,我不是做正确。

解决方案

您需要在shouldOverrideUrlLoading方法返回true

 公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
            view.loadUrl(URL);
            返回true;
        }
 

I have been researching for 2 days and I haven't found a similar problem anywhere on the internet!

From the main activity, I'm trying to open a new activity containing a webview that will show an html page (about.html) in the android_asset folder.

I'm able to launch the new activity (test.java) with the webview properly displayed but the webpage content just doesn't show up. Even the webview's vertical scrollbar is displayed (as the page content is relatively long) and I can scroll the page up and down but with no content in it!

package com.example.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class test extends Activity {
private TextView apppagetitle;
private WebView browser_rn;

@Override
public void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);

    setContentView(R.layout.test);

    browser_rn=(WebView)findViewById(R.id.webkit);
    browser_rn.getSettings().setJavaScriptEnabled(true);
    browser_rn.getSettings().setPluginsEnabled(true);
    browser_rn.setScrollBarStyle(0);       

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    apppagetitle=(TextView)findViewById(R.id.apppagetitle);


    browser_rn.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }

        public void onPageFinished(WebView view, String url) {               
            apppagetitle.setText(browser_rn.getTitle());                
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            if (!isInternetOn()) {
                alertDialog.setTitle("Connection Error");
                alertDialog.setMessage("You need to be connected to the internet.");
            } else {
                alertDialog.setTitle("Error");
                alertDialog.setMessage(description);
            }
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });
            alertDialog.show();
        }
    });

    browser_rn.loadUrl("file:///android_asset/about.html");
}

public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        browser_rn.clearCache(true);
        browser_rn.clearHistory();
        this.finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
}

The weird thing is that the new activity (test.java) can successfully get and display the webpage title in the textview when I call webview.gettitle() but the html content just doesn't render in the webview. All I see is a blank screen with scrollbars. I suspect it is the boolean "shouldOverrideUrlLoading" that might be causing it.

Update:

Thanks for replying.

test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.test"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/green"
>
<TextView   
android:id="@+id/apppagetitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="Odd News"
android:textSize="25px"
android:padding="10px"
/> 
</LinearLayout>
<LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<WebView  
android:id="@+id/webkit"
android:layout_width="fill_parent" 
android:layout_height="0px"
android:layout_weight="1"
/> 
</LinearLayout>
</LinearLayout>

about.html is as simple as what is below.

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8" /> 
<title>About</title> 
</head> 
<body> 
<h3 style="color:#0066dd">About</h3>  

<p>Some text here...</p>

</body> 
</html>

Oh yes, and I forgot to mention earlier that with the same piece of code, the about.html would sometimes show up if I force-stop and restart the app on my phone. However, it would then not appear in subsequent launches. On occasions when it successfully shows up about.html and I press the back button to return to the main activity, it will disappear again when I re-launch the activity from the main activity. I'm lost but I have a strong feeling it is "shouldOverrideUrlLoading" that I'm not doing correctly.

解决方案

You need to return true in shouldOverrideUrlLoading method

public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

这篇关于打开新的Andr​​oid活动与web视图显示一个空白(白色)屏幕而不是HTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆