Butterknife的BindView片段@onclick NullPointError

时间:2017-08-22 08:05:48

标签: java android android-fragments butterknife

public class InputFragment extends Fragment {
    public ContractMVP.start start;
    void  setPresenter(ContractMVP.start start){
        this.start=start;
    }
    @BindView(R.id.Start_btn)Button btn;
    @OnClick(R.id.Start_btn) void setBtn() {
        start.start();
    }

    public static InputFragment newInstance() {
        return new InputFragment();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState); 
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_input, container, false);
        ButterKnife.bind(this,v);

        return v;
    }

当我尝试点击按钮时,我得到NullpointException。当我调试它时start=null, 但是当我使用传统的findViewByid时,它可以正常工作

1 个答案:

答案 0 :(得分:2)

你是否添加了两种依赖

  

编译'com.jakewharton:butterknife:8.8.1'   annotationProcessor'com.jakewharton:butterknife编译:8.8.1'

同时检查XML视图IdR.id.(yourId)