基本内部链接在蜂窝应用程序中不起作用?

时间:2011-07-01 01:12:39

标签: android hyperlink webview internal

在我发布的应用中,内部链接似乎无法在Android版本3中运行。我的应用程序目前针对Froyo。

该应用程序适用于大量手机,但我的新Galaxy Tab无法处理内部链接!!它可以在html页面中处理它们,即:

<a href="#faq">Go to faq</a>  <!-- goes to FAQ link -->

转到同一页面下方的标记:

<a name="faq" id="faq"></a>  

但是从另一个html文件,即索引页面,该链接不再适用于Honeycomb:

<a href="mainpage.html#faq">FAQ</a>  <!-- goes to error page -->

此外,如果我转到内部链接,并从那里按照指向另一个页面的链接,然后点击后退按钮,(它被覆盖以转到上一个webview页面)你得到相同的错误,即:

The webpage at file:///android_asset/folder/mainpage.html#faq might be temporarily    down or it may have moved permanently to a new web address

WTF! webview就在页面上,但是你在1秒后回击,它找不到它。也不能从另一个html页面链接,但它在1.x,2.x中都可以正常工作,只是没有3.1(还没试过3.0)

注意:我看到了这个几乎完全相同的问题: android_asset not working on Honeycomb? 但是我的资产路径中没有空格。

我尝试过使用和不使用webclient,并尝试使用DOM和缓存设置无济于事。这是我目前在oncreate中的一个例子:

        browser = new WebView(this);
//  browser = (WebView) findViewById(R.id.webkit);  // tried with XML and without
    browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setPluginsEnabled(true);
//  browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//  browser.getSettings().setUseWideViewPort(true);
    browser.setBackgroundColor(Color.parseColor("#333333"));
    browser.setInitialScale(1);
    browser.getSettings().setBuiltInZoomControls(true);


    final Activity MyActivity = this;
    browser.setWebChromeClient(new WebChromeClient() {

        public void onProgressChanged(WebView view, int progress) {
            // Make the bar disappear after URL is loaded, and changes
            // string to Loading...

            setProgressBarIndeterminateVisibility(true);

            MyActivity.setTitle("  Loading . . . " + progress + "%");
            MyActivity.setProgress(progress * 100); // Make the bar

            if (progress == 100) {
                setTitle("  APP TITLE YADA YADA");
                setProgressBarIndeterminateVisibility(false);
            }
        }
    });
    browser.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);  // note I tried with and without overriding this 
            return true;
        }

    });
    setContentView(browser);

    browser.loadUrl("file:///android_asset/folder/page.html");

2 个答案:

答案 0 :(得分:4)

这是我使用的解决方案。在所有Android平台上运行相同(在所有1.6到4.0.1上测试)

正常加载文件,没有锚点。 E.g:

webview.loadUrl("file:///android_asset/file.html");

加载没有锚点的URL(例如file:///android_asset/file.html),并添加:

webview.setWebViewClient(new WebViewClient()
{
    public void onPageFinished(WebView view, String url)
    {
        if (this.anchor != null)
        {
            view.loadUrl("javascript:window.location.hash='" + this.anchor + "'");
            this.anchor = null;
        }
    }
});

其中anchor是您要跳转到的锚点。

你必须确保启用了javascript:

WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);

这是用于将文件加载到webview中。 现在,如果你想要捕获现在被破坏的内部链接,正如另一个答案中所建议的那样,覆盖onReceivedError方法并插入类似的内容:

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
    {
        int found = failingUrl.indexOf('#');

        if (found > 0)
        {
            anchor = failingUrl.substring(found + 1, failingUrl.length());
            view.loadUrl(failingUrl.substring(0, found));
        }
    }

答案 1 :(得分:3)

这是我的解决方法代码,因此错误对用户是透明的 为浏览器定义WebViewClient,并为onReceivedError包含类似的内容:

        @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
    {
        if (failingUrl.contains("#")) {
            Log.v("LOG", "failing url:"+ failingUrl);
            final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
            if (sdkVersion > Build.VERSION_CODES.GINGERBREAD) {
                String[] temp;
                temp = failingUrl.split("#");
                view.loadUrl(temp[0]); // load page without internal link

                try {
                    Thread.sleep(400);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
            }

            view.loadUrl(failingUrl);  // try again
        } else {
             view.loadUrl("file:///android_asset/tableofcontents.html");
        }
    }
    });

这会欺骗webview首先加载没有#link的页面,然后睡眠.4秒钟,然后再次加载完整的URL。我选择仅通过sdkVersion为平板电脑做这个技巧。如果有任何其他错误,我加载另一个页面,tableofcontents.html。这可以解决我的Galaxy Tab上的问题。

相关问题