【问题标题】:Webview content not loading all devicesWebview 内容未加载所有设备
【发布时间】:2016-02-04 07:48:26
【问题描述】:

我正在使用 webview 将数据从服务器加载到设备。但它没有在像 Moto x 和某些设备加载的少数设备中加载,但是当从 webview 页面转到另一个页面并返回同一页面时,webview 内容丢失。这发生在 Xperia z 中。其他设备三星,小米一切工作正常。

是什么问题以及如何解决这个问题,以便 webview 数据呈现所有设备正常。

LOGCAT -:

[AdapterInputConnection.java:499] finishComposingText
 D/cr_Ime: [AdapterInputConnection.java:499] finishComposingText
 D/cr_Ime: [AdapterInputConnection.java:145] Constructor called with outAttrs:  inputType=0xa1 imeOptions=0x12000000 privateImeOptions=null
 D/cr_Ime: actionLabel=null actionId=0
 D/cr_Ime: initialSelStart=0 initialSelEnd=0 initialCapsMode=0x0
 D/cr_Ime: hintText=null label=null
 D/cr_Ime: packageName=com.org.AmarUjala.news fieldId=-1 fieldName=null
 D/cr_Ime: extras=null
 D/cr_Ime: [AdapterInputConnection.java:499] finishComposingText
 D/cr_Ime: [AdapterInputConnection.java:499] finishComposingText

这里是 Xml 代码

          <WebView
            android:id="@+id/storyText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:lineSpacingExtra="3dp"
            android:scrollbars="none"
            android:visibility="gone"
            android:textColor="@color/black" /> 

这是Java代码:

int currentApi = android.os.Build.VERSION.SDK_INT;
mWebView.setInitialScale(0);
mWebView.requestFocus(View.FOCUS_DOWN);
mWebView.requestFocusFromTouch();
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setDomStorageEnabled(true);
settings.setLoadsImagesAutomatically(true);
settings.setSupportZoom(false);
settings.setAppCacheEnabled(false);
settings.setSavePassword(false);
settings.setPluginState(WebSettings.PluginState.ON);
settings.setLoadsImagesAutomatically(true);
settings.setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadDataWithBaseURL("", ftext,"text/html",utf-8,"");              
mWebView.setVisibility(View.VISIBLE);

【问题讨论】:

    标签: android xml webview


    【解决方案1】:

    有些配置没有区别,也许您可​​以删除它们:

    mWebView.setInitialScale(0); // This is 0 by default scale
    
    settings.setAppCacheEnabled(false); // This is false by default
    
    settings.setLoadsImagesAutomatically(true); //This is twice
    settings.setLoadsImagesAutomatically(true); //This is twice
    
    settings.setPluginState(WebSettings.PluginState.ON); // This is deprecated since API level 18
    

    如果那些给您带来麻烦的设备在 API 级别 18+ 上运行,则上述行可能是问题所在。

    那么,尽管您正在使用此方法,但您似乎并未从 URL 加载内容:

    mWebView.loadDataWithBaseURL("", ftext,"text/html",utf-8,""); //The first parameter is empty and could lead to 'about:blank'
    

    如果您从远程服务器加载并且有一个 url,请使用 loadDataWithBaseURL,否则如果您从本地加载,则使用其他方法,例如: webview.loadData(summary, "text/html", null);

    编辑: 然后根据您最新的 cmets,我会将用户浏览的 Urls 保存在集合中,甚至将 scrollY 保存在这些特定设备中。然后听“后退”按钮并浏览这些设备中的保存 URL,如果需要,在每个页面上重新加载页面。

    【讨论】:

    • 我删除了所有东西,但问题仍然存在
    • 提供的日志没有显示任何与 WebView 相关的条目。从我的脑海中,我会删除所有设置,并使用带有正确 URL 的 loadDataWithBaseUrl。否则,如果您能提供网址,那么我可以自己查看。
    • 感谢 Proverbio,我删除了所有设置并使用 loadDataWithBase Url,但它的设备特定问题。大多数设备一切正常,但问题只有 Xperia 和 moto X 设备。第一次加载所有内容但渲染到webview 页面到另一个页面,然后按返回按钮来到 webview 页面,然后 webview 数据显示为空白,这是问题
    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 2019-09-03
    • 1970-01-01
    • 2015-09-07
    • 2012-06-28
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    相关资源
    最近更新 更多