2013-03-08 123 views
0

我想用setWebViewClient &加载一个页面setWebChromeClient。我可以成功加载google.com,twitter.com,使用JS + CSS + HTML创建的另一个网页,等等。但不可能正确加载此网页: http://mozilla.github.com/pdf.js/web/viewer.htmlAndroid:setWebViewClient&setWebChromeClient显示空白页

有没有人知道发生了什么事以及如何解决它?

下面的代码:

public class MyActivity extends Activity { 

     private WebView mWebView; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_shelf); 

     mWebView = (WebView) findViewById(R.id.webView); 

     mWebView.setWebChromeClient(new WebChromeClient(){ 
      public boolean onConsoleMessage(ConsoleMessage cm) { 
       Log.d(Utils.tConsole, cm.message() 
         + " -- From line " + cm.lineNumber() 
         + " of " + cm.sourceId() 
         ); 
       return true; 
      } 
     }); 
     mWebView.setWebViewClient(new WebViewClient(){ 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url){ 
       view.loadUrl(url); 
       return true; 
      } 
     }); 

     mWebView.getSettings().setLoadsImagesAutomatically(true); 
     mWebView.getSettings().setDatabaseEnabled(true); 
     mWebView.getSettings().setAppCacheEnabled(true); 
     mWebView.getSettings().setDomStorageEnabled(true); 
     mWebView.getSettings().setJavaScriptEnabled(true); 

     mWebView.loadUrl("http://mozilla.github.com/pdf.js/web/viewer.html"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_shelf, menu); 
     return true; 
    } 
} 

相应布局的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@android:color/black" 
> 
    <WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/webView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 
</LinearLayout> 

且相应的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.hey" 
    android:versionCode="1" 
    android:versionName="1.0" 
    > 

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 


    <application 
     android:allowBackup="true" 
     android:hardwareAccelerated="true" 
     android:screenOrientation= "landscape" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.hey.MyActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

任何帮助将非常感激! 谢谢。

回答

0

的第一个问题是shouldOverrideUrlLoading(),将其删除或替换为:

public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if (Uri.parse(url).getHost().equals("www.myHost.com")) { 
    // This is my web site, do not override; let WebView load the page 
     return false; 
    } 
    // Otherwise, the link is not for a page on my site 
    // Launch another Activity that handles URLs 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    startActivity(intent); 
    return true; 
    } 

现在有另外一个问题:web视图似乎是无法加载大型文件。 我可以显示小型网页,但大型网页永远无法加载。

有没有什么办法来“调整”WebView或建议?

+0

最近pdf.js渐进式加载的合并代码,但您可能需要使用git中的master – sherpya 2013-04-19 22:57:28