Android的web视图呈黑色 [英] android webview appears black

查看:130
本文介绍了Android的web视图呈黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着加入AdSense的code。在我建立一个应用程序。 Adsense的code
存储在我的网站上一个HTML文件。这是我怎么称呼它:

Im trying to add adsense code in one app that I have built. The adsense code is stored in a html file on my website. And this is how I call it:

WebView ads = (WebView) findViewById(R.id.topads);
ads.getSettings().setJavaScriptEnabled(true);
ads.loadUrl("http://www.site.com/code/adsense.html");

和上的活动主要XML:

And on activity main xml:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
   <WebView 
    android:id="@+id/topads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
</LinearLayout>

我试着设置LinearLayots到:
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
它的无效或者

在AndroidManifest.html我已经加入:

On AndroidManifest.html I have added:

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

我看到的是网页视图加载后一个黑色的容器。我试着这样的结果
Android模拟器,采用的是Android 4.4。结果
有人可以帮我吗?

All I see is a black container after webview loads. Im trying this on the
android emulator, using android 4.4.
Can someone help me?

先谢谢了。

推荐答案

使用 WebViewClient 。现在,应该在web视图单独打开的URL。我不知道自己的URL,你说的是工作。但是,你试试下面和它的作品。您可以尝试开放尝试不同的URL只是用于测试

Use a WebViewClient. Now it should open the url in webview itself. I am not sure of your own url that you say is working. But you try the below and it works. You can try opening try different url's just for testing

测试在仿真器API 19

Tested on emulator api 19

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    WebView web;
    ProgressBar progressBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        web = (WebView) findViewById(R.id.wv);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://slashdot.org/");
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            // TODO Auto-generated method stub
             Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            //super.onReceivedError(view, errorCode, description, failingUrl);
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
            web.goBack();
            return true;
        }
        else
        {
            finish();
            return true;
        }
    }
}

对齐

这篇关于Android的web视图呈黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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