ButterKnife与Handle使用控件报空指针问题

  

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        what = msg.what;
        if (what > 0) {
            what--;
            tv_yanzhengma_chongfa.setText(what + "s后重试");
            handler.sendEmptyMessageDelayed(what, 1000);
        } else {
            tv_yanzhengma_chongfa.setText("重发校验码");
            tv_yanzhengma_chongfa.setClickable(true);
            tv_yanzhengma_chongfa.setTextColor(getResources().getColor(R.color.colorMainBlue));
        }
    }
};

1.   短信验证码handle发送60s消息,页面返回时找不到handle里面的控件,报空指针异常。。。这时要在onCreateView()方法里面用findViewByid()注册控件,不能用ButterKnife注册控件。

   异常信息:

  • 1    Caused by: java.lang.NullPointerException
  • 2    at com.example.yumin.fragment.MyQianbaoBindCardYZPhoneFragment$4.handleMessage(MyQianbaoBindCardYZPhoneFragment.java:167)
  • 3    at android.os.Handler.dispatchMessage(Handler.java:102)
  • 4    at android.os.Looper.loop(Looper.java:157)
  • 5    at android.app.ActivityThread.main(ActivityThread.java:5293)
  • 6    at java.lang.reflect.Method.invokeNative(Native Method)
  • 7    at java.lang.reflect.Method.invoke(Method.java:515)
  • 8    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
  • 9    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
  • 10    at dalvik.system.NativeStart.main(Native Method)
2.当前页面退出时,handle还在发送消息, else里面的控件也 报空指针。。。这时要在onDestory()方法里面清除消息
  handler.removeMessages(what);

你可能感兴趣的:(ButterKnife与Handle使用控件报空指针问题)