我想将ViewStub与ButterKnife一起使用,这是我已经完成的工作:

public class ExampleFragment extends Fragment {

    @InjectView ( R.id.stub )
    ViewStub mStub;

    /* A TextView in the ViewStub */
    @InjectView ( R.id.text )
    @Optional
    TextView mText;

    @Override
    public View onCreateView ( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {

        View rootView = inflater.inflate ( R.layout.rootview, container, false );
        ButterKnife.inject ( this, rootView );

        mStub.setLayoutResource ( R.layout.stub_layout );
        View inflated = mStub.inflate ();
        ButterKnife.inject ( mStub, inflated );

        mText.setText("test.");

        return rootView;
    }
}

日志显示:

mText is a null object reference

我现在不知道,欢迎任何建议。谢谢!

最佳答案

这是 jack 针对此请求的答案:



有关代码,请参阅此Github问题。

关于android - 如何用ButterKnife注入(inject)ViewStub?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27652643/

10-08 21:57