11

Internal links do not seem to be working in Android version 3 in my published app. My app targets Froyo at this point.

The app works fine on tons of phones, but my new Galaxy Tab can't handle the internal links!! It can handle them within an html page, ie:

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

Goes to the tag lower on the same page:

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

However from a another html file, ie the index page, the link no longer works in Honeycomb:

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

Also, if I go to an internal link, and from there follow a link to another page, then hit the back button, (it is overridden to go to previous webview page) you get the same error ie:

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! The webview was just on the page, but you hit back 1 second later, and it can't find it. Nor can it link from another html page, but it all works fine in 1.x, 2.x, just not 3.1 (have not tried 3.0)

NOTE: I have seen this almost identical question: android_asset not working on Honeycomb? But there are no spaces in my asset path.

I have tried with and without the webclient, and tried the DOM and cache settings to no avail. Here is an example of what I currently have in 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");
4
  • I'm having the same problem, did you ever find a solution ? this site seems to talk about something similar: droidnova.com/honeycomb-webview-changes-and-issues,876.html, but it offers no solutions ...
    – user854837
    Jul 20, 2011 at 21:43
  • Months later, I still have no solution for this other than detecting 3.x OS and chopping the internal link off from the URL. The functionality is hampered, but it avoids the unfound error.
    – Mischa
    Aug 25, 2011 at 21:10
  • I am now doing the following as a nearly transparent workaround. In onReceivedError, which fires when the URL fails, I detect any os above gingerbread and chop off the internal link to get the basic web page URL. I then load that in the webview, and do a sleep delay for 1/2 a second. I then load the original failedURL with internal link and voila, it now loads. Apparently the browser is making the classic mistake of trying to access the link before it exists. If anyone has any more intel on this bug, I'm all ears!
    – Mischa
    Aug 26, 2011 at 10:17
  • There is a bug report as well: code.google.com/p/android/issues/detail?id=19293 — we should add our self to it. And it happens to me without any internal links…
    – Martin
    Jan 1, 2012 at 17:41

2 Answers 2

4

Here is the solution I use. Works the same on all Android platform (tested on all 1.6 to 4.0.1)

Load the file as you would normally, without the anchor point. E.g.:

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

Load the URL without the anchor point (e.g. file:///android_asset/file.html), and add:

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;
        }
    }
});

where anchor is the anchor point you want to jump to.

you have to make sure javascript is enabled:

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

This is for loading the file into the webview. Now if you want to catch internal links that are now broken, as suggested in the other answer, override the onReceivedError method and insert do something like:

    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));
        }
    }
0
3

Here is my workaround code so the bug is transparent to the user.
Define the WebViewClient for the browser, and include something like the following for 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");
        }
    }
    });

This tricks the webview to first load the page without the #link, then sleep for .4 of a second, and then load the full URL again. I have chosen to do this trick for tablets only by sdkVersion. If there is any other error I load another page, the tableofcontents.html. This works to fix the problem on my Galaxy Tab.

6
  • Not sure why this was voted down but thanks for nothing, the workaround does work. It can also work with other symbols (ie ? as opposed to #) If you are going to vote it down the least you can do is explain yourself!
    – Mischa
    Nov 15, 2011 at 1:04
  • This fixes the issue on our published app. It should not be necessary, but whoever voted it down really needs to explain instead of just hatin'
    – Mischa
    Nov 15, 2011 at 1:08
  • Tried if but it did not work. But then I had no internal links to start with. BTW: There is a bug report as well: code.google.com/p/android/issues/detail?id=19293
    – Martin
    Jan 1, 2012 at 17:54
  • If you had no internal links to start with, then I am not seeing how my workaround to internal links would work or not work?
    – Mischa
    Jan 4, 2012 at 7:06
  • It did not. There is another (unrelated) Problem: stackoverflow.com/a/8694428/341091
    – Martin
    Jan 4, 2012 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.