2016-09-27 10944 views
0

我刚开始使用android编码,我仍然从错误中学习。我使用WebView加载内部html页面,我想要打开另一个活动窗口,该窗口将是barcode scanner,方法是单击webview上的超链接。不过,我得到这个错误无法打开资源URL:file:/// android_asset/activity_a

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


     } 

回答

1

WebView不知道activity_a://是什么。显然,它将其视为相对参考,就好像它是activity_a/

由于您在WebView中使用这个,所以不需要创建自己的方案。您正在检查整个网址shouldOverrideUrlLoading()

所以,你可以改变HTML到:

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

,改变你的if匹配:

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

而且,你可以从你的<activity>摆脱<intent-filter>的。无论如何,这表示设备上的任何应用程序都可以启动该活动,因为这是危险的,因为导出了<intent-filter>的活动。

+0

你好,非常感谢你的回答。我做了你的变化,但我仍然得到相同的错误。 file:/// qrcodeactivity找不到 – zontrakulla

+0

@zontrakulla:对不起,我在'if'测试中忘了这个方案。查看更新后的答案。基本上,你的'if'需要匹配'WebView'生成的URL。 – CommonsWare

+0

工作完美!非常感谢。 – zontrakulla

相关问题