2016-09-27 73 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

相關問題