找不到〜ItemBinding〜的设置器,该设置器接受参数类型'androidx.lifecycle.LiveData <

问题描述

找不到用于的设置器 接受参数类型 'androidx.lifecycle.LiveData ' 如果绑定适配器提供了设置器,请检查适配器是否为 正确注释并且参数类型匹配。

我已经逐行浏览了我的应用程序超过两天了。我使用Google提供的“ BasicSample” Android Room应用程序来模拟自己的应用程序,但是当我将其包含在dog_fragment.xml中时出现此错误

<include
 layout="@layout/dog_item"
 app:product="@{dogViewModel.dog}" />

“ dog_item”布局(dog_item.xml)用于显示狗的列表,当您单击它时,它将带您进入狗详细信息屏幕(dog_fragment.xml)。没有它,一切都将运行良好,但是它会丢失“狗”图块以进入详细信息屏幕,并且只会显示chew_toys的列表。

dog_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
  
  <data>
    <import type="android.view.View" />
    
    <variable
      name="isLoading"
      type="boolean" />
    
    <variable
      name="dogViewModel"
      type="the_derek.dogstuff.viewmodel.DogViewModel" />
  </data>
  
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/cardview_light_background"
    android:orientation="vertical">
    
    <include
      layout="@layout/dog_item"
      app:product="@{dogViewModel.dog}" />
    
    <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      <TextView
        android:id="@+id/tv_loading_chew_toys"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/loading_chew_toys"
        app:visibleGone="@{isLoading}" />
      
      <FrameLayout
        android:id="@+id/chew_toys_list_wrapper"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <androidx.recyclerview.widget.RecyclerView
          android:id="@+id/chew_toy_list"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:contentDescription="@string/cd_chew_toys_list"
          app:layoutManager="LinearLayoutManager"
          app:visibleGone="@{!isLoading}" />
      </FrameLayout>
    </FrameLayout>
  </LinearLayout>
</layout>

DogFragment.java

public class DogFragment extends Fragment {

 private static final String TAG = "\t\tDogFragment";
 private static final String KEY_DOG_ID = "dog_id";

 private final ChewToyClickCallback mChewToyClickCallback =
   chewToy -> {
    // no-op
   };
 private DogFragmentBinding mBinding;
 private ChewToyAdapter mChewToyAdapter;

 public static DogFragment forDog(int dogId) {
  DogFragment fragment = new DogFragment();
  Bundle args = new Bundle();
  args.putInt(KEY_DOG_ID,dogId);
  fragment.setArguments(args);
  return fragment;
 }

 @Nullable
 @Override
 public View onCreateView(
   @NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
  mBinding = DataBindingUtil.inflate(inflater,R.layout.dog_fragment,container,false);
 
  mChewToyAdapter = new ChewToyAdapter(mChewToyClickCallback);
  mBinding.chewToyList.setAdapter(mChewToyAdapter);
  return mBinding.getRoot();
 }

 @Override
 public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
  DogViewModel.Factory factory =
    new DogViewModel.Factory(
      requireActivity().getApplication(),requireArguments().getInt(KEY_DOG_ID));
  final DogViewModel model =
    new ViewModelProvider(this,factory).get(DogViewModel.class);

  mBinding.setLifecycleOwner(getViewLifecycleOwner());
  mBinding.setDogViewModel(model);

  subscribeToModel(model);
 }

 private void subscribeToModel(final DogViewModel model) {
  model
    .getChewToys()
    .observe(
      getViewLifecycleOwner(),chewToyEntities -> {
       if (chewToyEntities != null) {
        mBinding.setIsLoading(false);
        mChewToyAdapter.submitList(chewToyEntities);
       } else {
        mBinding.setIsLoading(true);
       }
      });
 }

 @Override
 public void onDestroyView() {
  mBinding = null;
  mChewToyAdapter = null;
  super.onDestroyView();
 }
}

DogViewModel.java

public class DogViewModel extends AndroidViewModel {

  private static final String TAG = "\t\tDogViewModel";
  private final LiveData<DogEntity> mObservableDog;
  private final LiveData<List<ChewToyEntity>> mObservableChewToys;

  public DogViewModel(
      @NonNull Application application,DataRepository repository,final int dogId) {
    super(application);
    mObservableChewToys = repository.loadChewToysById(dogId);
    mObservableDog = repository.loadDog(dogId);
  }

  public LiveData<List<ChewToyEntity>> getChewToys() {
    return mObservableChewToys;
  }

  public LiveData<DogEntity> getDog() {
    return mObservableDog;
  }

  public static class Factory extends ViewModelProvider.NewInstanceFactory {

    @NonNull private final Application mApplication;

    private final int mDogId;

    private final DataRepository mRepository;

    public Factory(@NonNull Application application,int dogId) {
      mApplication = application;
      mDogId = dogId;
      mRepository = ((DogApp) application).getRepository();
    }

    @SuppressWarnings("unchecked")
    @Override
    @NonNull
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
      return (T) new DogViewModel(mApplication,mRepository,mDogId);
    }
  }
}

BindingAdapters.java

public class BindingAdapters {
  @BindingAdapter("visibleGone")
  public static void showHide(View view,boolean show) {
    view.setVisibility(show ? View.VISIBLE : View.GONE);
  }
}

DogClickCallback.java

public interface DogClickCallback {
 void onClick(Dog dog);
}

dao查询

@Query("select * from dog_table where id = :dogId")
LiveData<DogEntity> loadDog(int dogId);

DogAdapter.java

public class DogAdapter extends RecyclerView.Adapter<DogAdapter.DogViewHolder> {
 private static final String TAG = "\t\tDogAdapter";

 @Nullable private final DogClickCallback mDogClickCallback;
 List<? extends Dog> mDogList;

 public DogAdapter(@Nullable DogClickCallback clickCallback) {
   Log.i(TAG,"DogAdapter: public constructor");
   mDogClickCallback = clickCallback;
   setHasStableIds(true);
 }

 public void setDogList(final List<? extends Dog> dogList) {
   if (mDogList == null) {
    mDogList = dogList;
    notifyItemRangeInserted(0,dogList.size());
   } else {
    DiffUtil.DiffResult result =
       DiffUtil.calculateDiff(
          new DiffUtil.Callback() {
            @Override
            public int getOldListSize() {
             return mDogList.size();
            }
            @Override
            public int getNewListSize() {
             return dogList.size();
            }
            @Override
            public boolean areItemsTheSame(int oldItemPosition,int newItemPosition) {
             return mDogList.get(oldItemPosition).getId()
                == dogList.get(newItemPosition).getId();
            }
            @Override
            public boolean areContentsTheSame(int oldItemPosition,int newItemPosition) {
             Dog newDog = dogList.get(newItemPosition);
             Dog oldDog = mDogList.get(oldItemPosition);
             return newDog.getId() == oldDog.getId()
                && TextUtils.equals(newDog.getName(),oldDog.getName());
            }
          });
    mDogList = dogList;
    result.dispatchUpdatesTo(this);
   }
 }

 @Override
 @NonNull
 public DogViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
   DogItemBinding binding =
      DataBindingUtil.inflate(
         LayoutInflater.from(parent.getContext()),R.layout.dog_item,parent,false);
   binding.setCallback(mDogClickCallback);
   return new DogViewHolder(binding);
 }

 @Override
 public void onBindViewHolder(@NonNull DogViewHolder holder,int position) {
   holder.binding.setDog(mDogList.get(position));
   holder.binding.executePendingBindings();
 }

 @Override
 public int getItemCount() {
   return mDogList == null ? 0 : mDogList.size();
 }

 @Override
 public long getItemId(int position) {
   return mDogList.get(position).getId();
 }

 static class DogViewHolder extends RecyclerView.ViewHolder {
   final DogItemBinding binding;
   public DogViewHolder(DogItemBinding binding) {
    super(binding.getRoot());
    this.binding = binding;
   }
 }
}


(DogEntity也有Dog模型类,如果有帮助的话) 我已经尝试过使缓存无效/重新启动,我已经尝试过清理项目,重建项目。我已经开始一个新项目并将其文件复制到其中。 哦,也是,这是另外一个错误:

import the_derek.dogstuff.databinding.DogFragmentBindingImpl;

它告诉我无法解决 DogFragmentBindingImpl 我不知道它是如何生成的,但是我认为问题是相互交织的。我不知道我是否错过任何有帮助的代码,请告诉我。

(仿照) android architecture-components-samples

解决方法

您要在此处传递LiveData对象:

app:product="@{dogViewModel.dog}"

您需要传递:

app:product="@{dogViewModel.dog.value}"
,

所以我每天花6-8个小时在这个上面,因为谁知道要花多长时间逐行遍历我的代码,而它又神奇地修复了自己的...某种。

我放弃了希望,打算将精力转移到其他地方,并尝试解决该问题,因此我从驱动器中删除了项目文件夹,然后从版本控制(从我的个人项目)中删除了New-> Project和错误不再存在。即使重建了项目,清理了缓存等等,这也以某种方式解决了该问题。无论我如何更改代码或XML文件,它都一定不想重建或重新创建BTS文件,但是当它感觉到一个全新的项目时,它们便是新的。

也许这是Android Studio中的错误?

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...