【问题标题】:Unable to open asset URL: file:///android_asset/activity_a无法打开资产 URL:file:///android_asset/activity_a
【发布时间】:2016-09-27 20:17:34
【问题描述】:

我刚开始编写 Android 代码,但我仍在从错误中吸取教训。我使用WebView 加载内部html 页面,我想通过单击webview 上的超链接打开另一个活动窗口,该窗口将是barcode scanner。但是我收到此错误

Unable to open asset URL: file:///android_asset/activity_a://qrcodeactivity

AndroidManifest.xml

<activity android:name="qrcodeactivity" >
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.VIEW" />
                <data android:scheme="activity_a" />
            </intent-filter>
        </activity>

index.html

<a href="activity_a://qrcodeactivity">Activity A</a>

MyWebClient Java

    private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.equals("activity_a://qrcodeactivity")) {
            Intent intent = new Intent(getContext(), qrcodeactivity.class);
            startActivity(intent);
            return true; // Handle By application itself
        } else {
            view.loadUrl(url);

            if (loader.equals("pull")) {
                swipeContainer.setRefreshing(true);
            } else if (loader.equals("dialog")) {
                if (!pd.isShowing()) {
                    pd.show();
                }
            } else if (loader.equals("never")) {
                Log.d("WebView", "No Loader selected");
            }

            return true;
        }


    }

    @Override
    public void onPageFinished(WebView view, String url) {
        if (pd.isShowing()) {
            pd.dismiss();
        }

        if (swipeContainer.isRefreshing()) {
            swipeContainer.setRefreshing(false);
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        webView.loadUrl("file:///android_asset/" + getString(R.string.error_page));
    }


        }

【问题讨论】:

    标签: android


    【解决方案1】:

    WebView 不知道activity_a:// 是什么。显然,它把它当作一个相对引用,就好像它是activity_a/

    由于您在WebView 中使用它,因此无需发明自己的方案。您正在检查 shouldOverrideUrlLoading() 中的整个 URL。

    因此,您可以将 HTML 更改为:

    <a href="/qrcodeactivity">Activity A</a>
    

    并更改您的 if 以匹配:

    if (url.equals("file:///qrcodeactivity")) {
    

    而且,您可以从您的 &lt;activity&gt; 中删除 &lt;intent-filter&gt;。无论如何,这样做很危险,因为您表示设备上的任何应用程序都可以启动该活动,因为带有&lt;intent-filter&gt; 的活动被导出。

    【讨论】:

    • 您好,非常感谢您的回答。我做了你的改变,但我仍然得到同样的错误。找不到文件:///qrcodeactivity
    • @zontrakulla:对不起,我忘记了if 测试中的方案。请参阅更新的答案。基本上,您的if 需要匹配WebView 生成的URL。
    • 完美运行!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    相关资源
    最近更新 更多