基本的内部链接在蜂窝应用程序中不起作用?
在我发布的应用程序中,内部链接似乎不适用于 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 无法在 Honeycomb 上运行? 但我的资产路径中没有空格。
我尝试过使用和不使用网络客户端,并尝试了 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");
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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我使用的解决方案。在所有 Android 平台上的工作方式相同(在所有 1.6 到 4.0.1 上进行测试)
像平常一样加载文件,无需锚点。例如:
加载不带锚点的URL(例如file:///android_asset/file.html),并添加:
其中anchor是你要跳转到的锚点。
你必须确保启用了 javascript:
这是为了将文件加载到 web 视图中。
现在,如果您想捕获现在已损坏的内部链接(如其他答案中所建议的那样),请覆盖 onReceivedError 方法并插入执行以下操作:
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.:
Load the URL without the anchor point (e.g. file:///android_asset/file.html), and add:
where anchor is the anchor point you want to jump to.
you have to make sure javascript is enabled:
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:
这是我的解决方法代码,因此该错误对用户来说是透明的。
为浏览器定义 WebViewClient ,并为 onReceivedError 包含类似以下内容:
这会欺骗 webview 首先加载没有 #link 的页面,然后休眠 0.4 秒,然后再次加载完整的 URL。我选择仅通过 sdkVersion 对平板电脑执行此操作。如果有任何其他错误,我将加载另一个页面,即 tableofcontents.html。这可以解决我的 Galaxy Tab 上的问题。
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:
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.