WebView在页面加载后显示白屏

时间:2014-07-21 20:16:07

标签: android android-webview

我有一个WebView应该加载网站的移动版本,但是当我尝试在WebView中加载它时,它只显示为白色屏幕。我尝试在我的Android浏览器中加载相同的URL,并在我的桌面上使用User-Agent Switcher加载。我知道页面也已完成加载,因为我有一个onPageFinished()方法,在显示页面时显示一个toast。每次加载应用程序时,我都会看到一个白色屏幕,然后几秒钟后,Toast消息。

编辑:我尝试了不同的移动网站,他们都在WebView中工作,唯一一个给我带来麻烦的是这一个:https://zangleweb01.clovisusd.k12.ca.us/StudentConnect/Home/Login

MainActivity.java

package com.student.connect;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.net.Uri;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements
        ActionBar.OnNavigationListener {

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current dropdown position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
    @SuppressWarnings("unused")
    private WebView student_zangle;

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

        // Set up the action bar to show a dropdown list.
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        // Set up the dropdown list navigation in the action bar.
        actionBar.setListNavigationCallbacks(
        // Specify a SpinnerAdapter to populate the dropdown list.
                new ArrayAdapter<String>(getActionBarThemedContextCompat(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1, new String[] {
                                getString(R.string.title_section1),
                                getString(R.string.title_section2),
                                getString(R.string.title_section3), }), this);
    }

    /**
     * Backward-compatible version of {@link ActionBar#getThemedContext()} that
     * simply returns the {@link android.app.Activity} if
     * <code>getThemedContext</code> is unavailable.
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private Context getActionBarThemedContextCompat() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return getActionBar().getThemedContext();
        } else {
            return this;
        }
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current dropdown position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current dropdown position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
                .getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @SuppressLint("NewApi")
    @Override
    public boolean onNavigationItemSelected(int position, long id)
    {
        WebView student_zangle = (WebView) findViewById(R.id.student_zangle);
        student_zangle.setWebViewClient( new YourWebClient());
        student_zangle.loadUrl("https://zangleweb01.clovisusd.k12.ca.us/StudentConnect/Home/Login");
        student_zangle.setDrawingCacheEnabled(false);
        WebSettings settings = student_zangle.getSettings();
        student_zangle.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setAllowContentAccess(true);

        student_zangle.setDownloadListener(new DownloadListener()
        { 
            public void onDownloadStart(String url, String userAgent, 
                    String contentDisposition, String mimetype, long contentLength)
            { 
                Intent intent = new Intent(Intent.ACTION_VIEW); 
                intent.setType("application/x-rar-compressed"); 
                intent.setData(Uri.parse(url)); 
                startActivity(intent); 
            } 
        }); 
        return true;
    }


    @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
          moveTaskToBack(true);
        }
        return super.onKeyDown(keyCode, event);
      }

    private class YourWebClient extends WebViewClient {     
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("mailto"))
            {
                String mail = url.replaceFirst("mailto:", "");
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("message/rfc822");
                intent.putExtra(Intent.EXTRA_EMAIL, mail );
                startActivity(intent);
                return true;            
            }
            view.loadUrl(url);
            return true;
        }
        public void onPageFinished(WebView view, String url)
        {
            Toast.makeText(getApplicationContext(), "done", 
                       Toast.LENGTH_LONG).show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试buildDrawingCache并进行以下更改:

student_zangle.setDrawingCacheEnabled(true);
student_zangle.buildDrawingCache();

注意:启用硬件加速时应避免调用此方法