Android:setWebViewClient& setWebChromeClient显示空白页面

时间:2013-03-08 11:52:12

标签: javascript android webview

我正在尝试使用setWebViewClient&amp ;;加载页面setWebChromeClient。我可以成功加载google.com,twitter.com,我用JS + CSS + HTML创建的另一个网页,依此类推。但无法正确加载此网页: http://mozilla.github.com/pdf.js/web/viewer.html

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

这里是代码:

    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:

<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>

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

1 个答案:

答案 0 :(得分: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;
  }

现在还有另一个问题:webview似乎无法加载大文件。 我可以显示小网页,但大页面永远不会加载。

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

相关问题