【问题标题】:Unable to use in local variable '@BindViews' not applicable to local variable butterknife无法在局部变量“@BindViews”中使用不适用于局部变量黄油刀
【发布时间】:2021-07-02 20:11:59
【问题描述】:
    public class LoginActivity extends AppCompatActivity {

  @BindViews(value = {R.id.logo, R.id.first, R.id.second, R.id.last})
  protected List<ImageView> sharedElements;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    ButterKnife.bind(this);
    @BindViews({R.id.pager)

    @BindViews(R.id.scrolling_background)
    final AnimatedViewPager pager = ButterKnife.findById(this, R.id.pager);
    final ImageView background = ButterKnife.findById(this, R.id.scrolling_background);
    int[] screenSize = screenSize();

    for (ImageView element : sharedElements) {
      @ColorRes int color = element.getId() != R.id.logo ? R.color.white_transparent : R.color.color_logo_log_in;
      DrawableCompat.setTint(element.getDrawable(), ContextCompat.getColor(this, color));
    }

验证片段

{
    public abstract class AuthFragment extends Fragment {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
      }
      protected Callback callback;
    
      @BindView(R.id.caption)
      protected VerticalTextView caption;
    
      @BindView(R.id.root)
      protected ViewGroup parent;
    
      protected boolean lock;
    
    
    
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(authLayout(), container, false);
        ButterKnife.bind(this, root);
        KeyboardVisibilityEvent.setEventListener(getActivity(), isOpen -> {
          callback.scale(isOpen);
          if (!isOpen) {
            clearFocus();
          }
        });
        return root;
      }
    
      public void setCallback(@NonNull Callback callback) {
        this.callback = callback;
      }
    
      @LayoutRes
      public abstract int authLayout();
    
      public abstract void fold();
    
      public abstract void clearFocus();
    
      @OnClick(R.id.root)
      public void unfold() {
        if (!lock) {
          caption.setVerticalText(false);
          caption.requestLayout();
          Rotate transition = new Rotate();
          transition.setStartAngle(-90f);
          transition.setEndAngle(0f);
          transition.addTarget(caption);
          TransitionSet set = new TransitionSet();
          set.setDuration(getResources().getInteger(R.integer.duration));
          ChangeBounds changeBounds = new ChangeBounds();
          set.addTransition(changeBounds);
          set.addTransition(transition);
          TextSizeTransition sizeTransition = new TextSizeTransition();
          sizeTransition.addTarget(caption);
          set.addTransition(sizeTransition);
          set.setOrdering(TransitionSet.ORDERING_TOGETHER);
          caption.post(() -> {
            TransitionManager.beginDelayedTransition(parent, set);
            } 

请帮我用黄油刀修复这个错误。 findbyid 也不可用,无法使用 bindview 请帮助提供文档或代码。因此,在 GermliShx 建议的一些更改之后,现在就是 authFragment 这些错误可能是我的实现错误,所以请帮助我。这是带有一些动画的登录页面,idk y butterknife 正在给她制造问题。我尝试解决它但没有运气这个错误是由于黄油刀的错误实施所以请帮帮我

【问题讨论】:

  • @BindViews({R.id.pager)?为什么在方法内部?

标签: android android-studio android-fragments butterknife


【解决方案1】:

首先删除这个:

    @BindViews({R.id.pager)
    @BindViews(R.id.scrolling_background)
... 
final AnimatedViewPager pager = ButterKnife.findById(this, R.id.pager);
    final ImageView background = ButterKnife.findById(this, R.id.scrolling_background);

第二次添加:

    public class LoginActivity extends AppCompatActivity {
        ...
       //binding is declared outside OnCreate / any other function

       //your case
        @BindView(R.id.pager)
            AnimatedViewPager pager;
            @BindView(R.id.scrolling_background)
            ImageView background ;

        ..
        //then in code just use declared pager and background when needed 
         @Override
          protected void onCreate(Bundle savedInstanceState) {
              background .setColorFilter (R.color.black); //example 
        }

应该可以,但我不确定 AnimatedViewPager 是什么。

它也可能显示Resource IDs will be non-final in Android Gradle Plugin version 7.0, avoid using them as annotation attributes 警告,但您可以直接取消它。

尝试并回复进展如何。

【讨论】:

  • imgur.com/a/dDmSrWm 检查这个 ss 兄弟我已经添加了这个有问题的代码,
  • @GermlinShx 谢谢哥们,现在出现这些错误
  • @anshulraj 尝试在 View Root 中将 authlayout() 更改为 R.layout.name_fragment。因为当编译器在你给他的布局中找不到你的 id 时,就会发生这个错误。
  • 我尝试并阅读了许多关于黄油刀的文档,但仍然无法解决它,所以请它谦虚的请求你能一次通过我的代码请谦虚的请求我尝试了我的整个大脑,但没有成功我还在学习,它是我的学校项目wetransfer.com/downloads/…(这里的完整代码请检查谦虚的请求)
  • 你可以通过 anshul348567@gmail.com 给我发邮件,请给我一个小的登录代码,然后是一个片段活动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 2021-06-10
  • 2019-07-24
  • 2017-11-08
相关资源
最近更新 更多