android – 使用一段时间后,我的应用程序在滚动WebView期间冻结,说“无法锁定表面”

前端之家收集整理的这篇文章主要介绍了android – 使用一段时间后,我的应用程序在滚动WebView期间冻结,说“无法锁定表面”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的 Android应用程序包含几个活动,每个活动负责单个片段(现在).我的碎片通常显示/附加有点像这样:
mTopicFragment = (TopicFragment)getSupportFragmentManager().findFragmentByTag("topic");
if(mTopicFragment == null)
    mTopicFragment = TopicFragment.newInstance(bid,page,pid);

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.content,mTopicFragment,"topic")
            .commit();
}

TopicFragment包含一个显示一些HTML和CSS / JS东西的WebView.经过一段时间浏览应用程序,其中一个TopicFragment WebView中的滚动变得缓慢,最终,应用程序完全冻结. ADB日志显示以下异常:

12-12 22:49:33.931  12582-12582/com.mde.potdroid3 W/Adreno-EGLSUB﹕ <DequeueBuffer:606>: dequeue native buffer fail: Unknown error 2147483646,buffer=0x0,handle=0x0
12-12 22:49:33.941  12582-12582/com.mde.potdroid3 W/Adreno-EGLSUB﹕ <DequeueBuffer:606>: dequeue native buffer fail: Invalid argument,handle=0x0
12-12 22:49:33.941  12582-12582/com.mde.potdroid3 W/Adreno-ES20﹕ <gl2_surface_swap:43>: GL_OUT_OF_MEMORY
12-12 22:49:33.941  12582-12582/com.mde.potdroid3 W/Adreno-EGL﹕ <qeglDrvAPI_eglSwapBuffers:3597>: EGL_BAD_SURFACE
12-12 22:49:33.941  12582-12582/com.mde.potdroid3 W/HardwareRenderer﹕ EGL error: EGL_BAD_SURFACE
12-12 22:49:33.951  12582-12582/com.mde.potdroid3 W/HardwareRenderer﹕ Mountain View,we've had a problem here. Switching back to software rendering.

12-12 22:20:04.461  10081-10081/com.mde.potdroid3 E/Surface﹕ dequeueBuffer Failed (Unknown error 2147483646)
12-12 22:20:04.461  10081-10081/com.mde.potdroid3 E/ViewRootImpl﹕ Could not lock surface
    java.lang.IllegalArgumentException
            at android.view.Surface.nativeLockCanvas(Native Method)
            at android.view.Surface.lockCanvas(Surface.java:243)
            at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2435)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2409)
            at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2253)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1883)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5081)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

在互联网上,我只能找到有关这个例外的信息,人们有自己的视图.这里发生了什么?它可以与我的应用程序的内存消耗有关吗?看起来好像我每次调用上面的代码一样,新的TopicFragment被实例化,显示并推送到后面的堆栈.我该如何进一步调试这个行为?

还有一个信息:当我在开发者设置中启用重叠时,应用程序似乎使用了大量的cpu.可能是,当我离开他们的时候,我的碎片没有正确分离,并且由于某种原因在后台运行?

这是我如何使用WebView:

mWebView = (WebView)getView().findViewById(R.id.topic_webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.addJavascriptInterface(mJsInterface,"api");
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadData("","text/html","utf-8");
mWebView.setBackgroundColor(0x00000000);

这不是内存泄漏提到here.

解决方法

我想我修正了显然,有一个bug阻止了WebView的正确的内存管理.当我们在视图中使用WebView启动许多活动时,WebViews会在内存中运行,并且当内存不足时活动没有被正确的杀死.我在TopicFragment中显示了WebView的以下代码,解决了这个问题:
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle saved) {
    View v = super.onCreateView(inflater,container,saved);

    mActivity = (BaseActivity) getSupportActivity();

    // this is the framelayout which will contain our WebView
    mWebContainer = (FrameLayout) v.findViewById(R.id.web_container);

    return v;
}

public void onResume() {
    super.onResume();

    // create new WebView and set all its options.
    mWebView = new WebView(mActivity);
    mWebView....

    // add it to the container
    mWebContainer.addView(mWebView);

    // if data is available,display it immediately
    if(mTopic != null) {
        mWebView.loadDataWithBaseURL("file:///android_asset/",mTopic.getHtmlCache(),"UTF-8",null);
    }
}

@Override
public void onPause() {
    super.onPause();

    // destroy the webview
    mWebView.destroy();
    mWebView = null;

    // remove the view from the container.
    mWebContainer.removeAllViews();
}

这样,在OnResume和onPause中创建和删除WebView.这是一些开销并不完美,但它解决了内存问题,在性能等方面几乎没有显着的.

猜你在找的Android相关文章