Fragment中使用ViewFlipper在严格模式下抛出android.app.IntentReceiverLeaked

来源:互联网 发布:语音软件 编辑:程序博客网 时间:2024/06/16 05:02

今天遇到一个bug:IntentReceiverLeaked, 是在严格模式下报的:

12-20 01:04:17.503 E/StrictMode(14464): android.app.IntentReceiverLeaked: Activity com.video.test.DetailActivity has leaked IntentReceiver android.widget.ViewFlipper$1@42b37fc0 that was originally registered here. Are you missing a call to unregisterReceiver()?12-20 01:04:17.503 E/StrictMode(14464): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:818)12-20 01:04:17.503 E/StrictMode(14464): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:603)12-20 01:04:17.503 E/StrictMode(14464): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1404)12-20 01:04:17.503 E/StrictMode(14464): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1384)12-20 01:04:17.503 E/StrictMode(14464): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1378)12-20 01:04:17.503 E/StrictMode(14464): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:446)12-20 01:04:17.503 E/StrictMode(14464): at android.widget.ViewFlipper.onAttachedToWindow(ViewFlipper.java:93)12-20 01:04:17.503 E/StrictMode(14464): at android.view.View.dispatchAttachedToWindow(View.java:12125)12-20 01:04:17.503 E/StrictMode(14464): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2453)12-20 01:04:17.503 E/StrictMode(14464): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460)12-20 01:04:17.503 E/StrictMode(14464): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460)12-20 01:04:17.503 E/StrictMode(14464): at android.view.ViewGroup.addViewInner(ViewGroup.java:3548)12-20 01:04:17.503 E/StrictMode(14464): at android.view.ViewGroup.addView(ViewGroup.java:3380)12-20 01:04:17.503 E/StrictMode(14464): at android.view.ViewGroup.addView(ViewGroup.java:3325)12-20 01:04:17.503 E/StrictMode(14464): at android.view.ViewGroup.addView(ViewGroup.java:3301)12-20 01:04:17.503 E/StrictMode(14464): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:922)12-20 01:04:17.503 E/StrictMode(14464): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)12-20 01:04:17.503 E/StrictMode(14464): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)12-20 01:04:17.503 E/StrictMode(14464): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)12-20 01:04:17.503 E/StrictMode(14464): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)12-20 01:04:17.503 E/StrictMode(14464): at android.os.Handler.handleCallback(Handler.java:730)12-20 01:04:17.503 E/StrictMode(14464): at android.os.Handler.dispatchMessage(Handler.java:92)12-20 01:04:17.503 E/StrictMode(14464): at android.os.Looper.loop(Looper.java:137)12-20 01:04:17.503 E/StrictMode(14464): at android.app.ActivityThread.main(ActivityThread.java:5265)12-20 01:04:17.503 E/StrictMode(14464): at java.lang.reflect.Method.invokeNative(Native Method)12-20 01:04:17.503 E/StrictMode(14464): at java.lang.reflect.Method.invoke(Method.java:525)12-20 01:04:17.503 E/StrictMode(14464): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:760)12-20 01:04:17.503 E/StrictMode(14464): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)12-20 01:04:17.503 E/StrictMode(14464): at dalvik.system.NativeStart.main(Native Method)


开始以为是Activity中的BroadcastReceiver没有调用unregisterReceiver,排查了代码发现都正常调用了。那应该是其他的问题了, 我试着检索了ViewFlipper,发现DetailActivity中有个Fragment用到了ViewFlipper, 网上查了下资料:

(http://www.eoeandroid.com/thread-159069-1-1.html)

大多是说重载onDetachedFromWindow,并在其中加入stopFlipping;或许我的情况与之不同,依然无效;

解决:重载Activity的onDetachedFromWindow在其中加入

if(mViewFlipper != null){    mViewFlipper .onDetachedFromWindow();}
即可


由于我使用的是Fragment, 后来在重载Fragment的onDetach:

@Override    public void onDetach() {        super.onDetach();        if(viewFlipper!=null){            //防止android.app.IntentReceiverLeaked            viewFlipper.stopFlipping();        }    }

发现OK了。



0 0
原创粉丝点击