对viewpager自定义适配器的destroyItem调用将转到unbinder.unbind()(使用ButterKnife)。如果滚动速度慢,它可以正常工作,不可见的视图正如预期的那样被破坏。但是如果快速滚动,它会崩溃以下(发现一些关于相同错误的答案,但无法从中得到任何东西,因为条件不同。例如我使用的不是碎片)
06-14 17:48:54.037 13718-13718/com.areaseys.seysconecta.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.areaseys.seysconecta.debug, PID: 13718
java.lang.IllegalStateException: Bindings already cleared.
at com.areaseys.seysconecta.biwidgets.cloud.accesscontrol.AccessControlWidgetView_ViewBinding.unbind(AccessControlWidgetView_ViewBinding.java:32)
at com.areaseys.seysconecta.mvp.view.BaseMVPViewView.onDetachedFromWindow(BaseMVPViewView.java:38)
at com.areaseys.seysconecta.biwidgets.asbtractwidget.AbstractWidgetView.onDetachedFromWindow(AbstractWidgetView.java:69)
at com.areaseys.seysconecta.mvp.view.BaseMVPViewPresenter.onDetachedFromWindow(BaseMVPViewPresenter.java:35)
at com.areaseys.seysconecta.mvp.view.BaseMVPView.onDetachedFromWindow(BaseMVPView.java:50)
at android.view.View.dispatchDetachedFromWindow(View.java:14519)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3133)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4497)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4470)
at android.view.ViewGroup.removeView(ViewGroup.java:4402)
at android.support.v4.view.ViewPager.removeView(ViewPager.java:1499)
at com.areaseys.seysconecta.biwidgets.cloud.accesscontrol.AccessControlPagerAdapter.destroyItem(AccessControlPagerAdapter.java:35)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1170)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:267)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:559)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
适配器:
public class AccessControlPagerAdapter extends PagerAdapter {
private ArrayList<AccessControlWidget> widgets;
public AccessControlPagerAdapter(ArrayList<AccessControlWidget> widgets) {
this.widgets = widgets;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
collection.addView(widgets.get(position));
return widgets.get(position);
}
@Override
public int getCount() {
return widgets.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
和ViewPager中包含的对象的基类:
public abstract class BaseMVPViewView<BASEVIEW extends BaseMVPView, ACTIONS extends ViewActions> {
protected final BASEVIEW baseview;
protected ACTIONS actions;
private Unbinder unbinder;
public BaseMVPViewView(BASEVIEW baseview) {
this.baseview = baseview;
actions = getDefaultActions();
}
protected abstract ACTIONS getDefaultActions();
public void bind() {
unbinder = ButterKnife.bind(this, baseview);
}
public void onAttachedToWindow() {
}
public void onDetachedFromWindow() {
unbinder.unbind();
}
public void onInit() {
}
public void setActions(@NonNull ACTIONS actions) {
this.actions = actions;
}
public void showError() {
Toast.makeText(baseview.getContext(), R.string.error_ocurred, Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
阅读完之后,找到了这个thread讨论问题的地方。我有想法捕获异常,看看会发生什么,我想现在有些听众仍然挂在内存中,没有清除。我的viewpager只使用4个视图,所以危险似乎很小,应用程序不再崩溃:
public void onDetachedFromWindow() {
try {
unbinder.unbind();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}