android:activity销毁后,fragment使用控件,空指针

fragment和activity绑定,activity退出,然后fragment的接口数据获取到了,使用控件属性,崩溃,按照正常来说,是不会空指针的,报的错是:java.lang.NullPointerException: Attempt to invoke virtual method ........... 

我的原意是使用了ButterKnife,他在unbind的时候会把控件属性置空,代码可以在ButterKnife自动生成的文件里看到,文件名是你的使用了ButterKnife的文件+$$ViewBinder, 比如

public class MsgPageFragment$$ViewBinder implements ViewBinder {
  @Override public void bind(final Finder finder, final T target, Object source) {
    View view;
    view = finder.findRequiredView(source, 2131231302, "field 'tvRead'");
    target.tvRead = finder.castView(view, 2131231302, "field 'tvRead'");
    view = finder.findRequiredView(source, 2131231250, "field 'tvMeetingTime'");
    target.tvMeetingTime = finder.castView(view, 2131231250, "field 'tvMeetingTime'");
    view = finder.findRequiredView(source, 2131231062, "field 'rlNotifyMeeting' and method 'onViewClicked'");
    target.rlNotifyMeeting = finder.castView(view, 2131231062, "field 'rlNotifyMeeting'");
    view.setOnClickListener(
      new butterknife.internal.DebouncingOnClickListener() {
        @Override public void doClick(
          android.view.View p0
        ) {
          target.onViewClicked(p0);
        }
      });
    view = finder.findRequiredView(source, 2131231310, "field 'tvRestaurantTime'");
    target.tvRestaurantTime = finder.castView(view, 2131231310, "field 'tvRestaurantTime'");
    view = finder.findRequiredView(source, 2131231063, "field 'rlNotifyRestaurant' and method 'onViewClicked'");
    target.rlNotifyRestaurant = finder.castView(view, 2131231063, "field 'rlNotifyRestaurant'");
    view.setOnClickListener(
      new butterknife.internal.DebouncingOnClickListener() {
        @Override public void doClick(
          android.view.View p0
        ) {
          target.onViewClicked(p0);
        }
      });
    view = finder.findRequiredView(source, 2131231337, "field 'tvServiceTime'");
    target.tvServiceTime = finder.castView(view, 2131231337, "field 'tvServiceTime'");
    view = finder.findRequiredView(source, 2131231065, "field 'rlNotifyService' and method 'onViewClicked'");
    target.rlNotifyService = finder.castView(view, 2131231065, "field 'rlNotifyService'");
    view.setOnClickListener(
      new butterknife.internal.DebouncingOnClickListener() {
        @Override public void doClick(
          android.view.View p0
        ) {
          target.onViewClicked(p0);
        }
      });
    view = finder.findRequiredView(source, 2131231313, "field 'tvSaleTime'");
    target.tvSaleTime = finder.castView(view, 2131231313, "field 'tvSaleTime'");
    view = finder.findRequiredView(source, 2131231064, "field 'rlNotifySale' and method 'onViewClicked'");
    target.rlNotifySale = finder.castView(view, 2131231064, "field 'rlNotifySale'");
    view.setOnClickListener(
      new butterknife.internal.DebouncingOnClickListener() {
        @Override public void doClick(
          android.view.View p0
        ) {
          target.onViewClicked(p0);
        }
      });
    view = finder.findRequiredView(source, 2131231244, "field 'tvMeetingMsgCount'");
    target.tvMeetingMsgCount = finder.castView(view, 2131231244, "field 'tvMeetingMsgCount'");
    view = finder.findRequiredView(source, 2131231308, "field 'tvRestaurantMsgCount'");
    target.tvRestaurantMsgCount = finder.castView(view, 2131231308, "field 'tvRestaurantMsgCount'");
    view = finder.findRequiredView(source, 2131231249, "field 'tvMeetingServiceMsgCount'");
    target.tvMeetingServiceMsgCount = finder.castView(view, 2131231249, "field 'tvMeetingServiceMsgCount'");
    view = finder.findRequiredView(source, 2131231309, "field 'tvRestaurantServiceCount'");
    target.tvRestaurantServiceCount = finder.castView(view, 2131231309, "field 'tvRestaurantServiceCount'");
    view = finder.findRequiredView(source, 2131231363, "field 'tvVisitorCount'");
    target.tvVisitorCount = finder.castView(view, 2131231363, "field 'tvVisitorCount'");
    view = finder.findRequiredView(source, 2131231365, "field 'tvVisitorTime'");
    target.tvVisitorTime = finder.castView(view, 2131231365, "field 'tvVisitorTime'");
    view = finder.findRequiredView(source, 2131231074, "method 'onViewClicked'");
    view.setOnClickListener(
      new butterknife.internal.DebouncingOnClickListener() {
        @Override public void doClick(
          android.view.View p0
        ) {
          target.onViewClicked(p0);
        }
      });
  }

  @Override public void unbind(T target) {
    target.tvRead = null;
    target.tvMeetingTime = null;
    target.rlNotifyMeeting = null;
    target.tvRestaurantTime = null;
    target.rlNotifyRestaurant = null;
    target.tvServiceTime = null;
    target.rlNotifyService = null;
    target.tvSaleTime = null;
    target.rlNotifySale = null;
    target.tvMeetingMsgCount = null;
    target.tvRestaurantMsgCount = null;
    target.tvMeetingServiceMsgCount = null;
    target.tvRestaurantServiceCount = null;
    target.tvVisitorCount = null;
    target.tvVisitorTime = null;
  }
}

可以看到,它在unbind把控件都置空了,知道了后解决也很容易了,可以在bind之后赋予一个全局标志位为true,比如

bind = true;

在unbind的前一行或者后一行 

bind = false;

然后在获取接口数据的地方加上

if(!bind) {

     return;

}

应该看得懂,很简单,这个空指针和activity是否绑定没关系的。

如果你在fragment里使用了activity,或者activity的context,导致空指针才和activity绑定有关系,附上一个activity销毁时,activity和绑定的fragment的生命周期

E/fragment: -- onPause--
E/activity: -- onPause--
E/fragment: --onStop--
E/activity: --onStop--
E/fragment: --onDestroyView--
E/fragment: --OnDestroy--
E/fragment: --OnDetach--
E/activity: --onDestroy--

 

你可能感兴趣的:(Android)