在 Fragment 中使用时出现 ButterKnife 错误

ButterKnife error when using in Fragment

我在 android 应用程序中使用 ButterKnife 库。它在活动中运行良好。但是当我在 Fragment 中使用它时,它在构建项目时给我一个错误。这是我的代码:

package com.foxastudios.stopnosocomials.Fragments;

public class FragmentObserveeOne extends Fragment {

    @BindView(R.id.text_obs_one_name) TextView obsName;

    public FragmentObserveeOne() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_observee_one, container, false);
        ButterKnife.bind(this,rootView);

        obsName.setText("TEXT");

        return rootView;
    }

}

我的片段在一个名为片段的单独包中。这是我得到的错误:

Error:(8, 39) error: cannot find symbol class Fragments
Error:(13, 65) error: package Fragments does not exist
Error:(27, 59) error: package Fragments does not exist

您的问题是包裹中包含一个大写字母。正如 Java 文档所描述的那样 - All packages should use lowercase letters.

Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

重命名包 fragments 应该可以解决您的问题。

最后,请查看最新的 Butterknife docs 以了解如何通过 Fragment 类 正确使用 Butterknife 以避免内存泄漏,因为您的代码目前不使用 [=12] =].文档的 'BINDING RESET' 部分应该为您指明正确的方向。