我试图创建一个能够在滚动时折叠工具栏并隐藏FAB的WebView
。我已经将所有功能实现,但只要我将WebView
设置为View.LAYER_TYPE_HARDWARE
,它就会崩溃。崩溃发生在Android 5.0及以上版本。在4.4以下版本中似乎可以正常工作。崩溃输出如下:
01-14 18:10:09.720 2941-3090/com.webviewtesthw D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-14 18:10:09.796 2941-3090/com.webviewtesthw I/OpenGLRenderer: Initialized EGL, version 1.4
01-14 18:10:09.886 2941-3097/com.webviewtesthw E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
01-14 18:10:10.583 2941-2941/com.webviewtesthw W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 2941
01-14 18:10:11.553 2941-2941/com.webviewtesthw W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 2941
01-14 18:10:11.727 2941-3090/com.webviewtesthw W/OpenGLRenderer: Layer exceeds max. dimensions supported by the GPU (1088x4288, max=4096x4096)
01-14 18:10:11.728 2941-3090/com.webviewtesthw D/OpenGLRenderer: Current memory usage / total memory usage (bytes):
TextureCache 57600 / 75497472
LayerCache 17825792 / 50331648 (numLayers = 1)
Layer size 1088x4096; isTextureLayer()=0; texid=7 fbo=0; refs=1
Layers total 17825792 (numLayers = 1)
RenderBufferCache 0 / 8388608
GradientCache 40960 / 1048576
PathCache 0 / 33554432
TessellationCache 0 / 1048576
TextDropShadowCache 0 / 6291456
PatchCache 0 / 131072
FontRenderer 0 A8 1048576 / 1048576
FontRenderer 0 RGBA 0 / 0
FontRenderer 0 total 1048576 / 1048576
Other:
FboCache 0 / 0
Total memory usage:
18972928 bytes, 18.09 MB
01-14 18:10:11.729 2941-2941/com.webviewtesthw D/AndroidRuntime: Shutting down VM
01-14 18:10:11.733 2941-2941/com.webviewtesthw E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.webviewtesthw, PID: 2941
java.lang.IllegalStateException: Unable to create layer for WebView
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我的代码如下:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.webviewtesthw.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.webviewtesthw.ScrollingActivity"
tools:showIn="@layout/activity_scrolling">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/webview"></WebView>
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
package com.webviewtesthw;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ScrollingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
WebView view = (WebView) findViewById(R.id.webview);
view.setWebViewClient(new WebViewClient());
WebSettings settings = view.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setAppCacheEnabled(true);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setBuiltInZoomControls(true);
settings.setAppCacheEnabled(true);
settings.setAppCachePath(getCacheDir().getAbsolutePath());
settings.setDatabaseEnabled(true);
settings.setSupportMultipleWindows(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setDomStorageEnabled(true);
settings.setAllowContentAccess(true);
settings.setAllowFileAccess(true);
settings.setSaveFormData(true);
// view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
view.loadUrl("http://vimeo.com");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_scrolling, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我做错了什么?
谢谢。
编辑:如果我将WebView
的高度和宽度设置为固定大小,则可以正常工作,但是一旦我将其更改为与父元素匹配或包裹内容,则会出现问题。如果去掉NestedScrollView
,则不会出现此问题。