无法找到接受参数类型为 ‘androidx.lifecycle.LiveData<' 的 ~ItemBinding~ 的设置器。

huangapple go评论115阅读模式
英文:

Cannot find a setter for ~ItemBinding~ that accepts parameter type 'androidx.lifecycle.LiveData<

问题

  1. 不能找到适用于 <the_derek.dogstuff.databinding.DogItemBinding app:product> 的设置器,该设置器接受参数类型 'androidx.lifecycle.LiveData<the_derek.dogstuff.db.entity.DogEntity>'。如果绑定适配器提供了设置器,请检查适配器的注释是否正确,并且参数类型是否匹配。
  2. 我已经逐行检查了我的应用程序超过两天了。我使用了 Google 提供的“BasicSample Android Room 应用程序来模拟我的应用程序,但是当我将它包含在我的 dog_fragment.xml 中时,出现了这个错误:
  3. <include
  4. layout="@layout/dog_item"
  5. app:product="@{dogViewModel.dog}" />
  6. dog_item 布局(dog_item.xml)用于显示狗的列表,当您点击它时,它会带您进入狗的详细信息屏幕(dog_fragment.xml)。没有它,一切都很正常,但是缺少“狗”图块以进入详细信息屏幕,只会显示一列咀嚼玩具。
  7. dog_fragment.xml
  8. <?xml version="1.0" encoding="utf-8"?>
  9. <layout xmlns:android="http://schemas.android.com/apk/res/android"
  10. xmlns:app="http://schemas.android.com/apk/res-auto">
  11. <data>
  12. <import type="android.view.View" />
  13. <variable
  14. name="isLoading"
  15. type="boolean" />
  16. <variable
  17. name="dog"
  18. type="the_derek.dogstuff.viewmodel.DogViewModel" />
  19. </data>
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android:background="@color/cardview_light_background"
  24. android:orientation="vertical">
  25. <include
  26. layout="@layout/dog_item"
  27. app:product="@{dogViewModel.dog}" />
  28. <FrameLayout
  29. android:layout_width="match_parent"
  30. android:layout_height="match_parent">
  31. <TextView
  32. android:id="@+id/tv_loading_chew_toys"
  33. android:layout_width="match_parent"
  34. android:layout_height="match_parent"
  35. android:text="@string/loading_chew_toys"
  36. app:visibleGone="@{isLoading}" />
  37. <FrameLayout
  38. android:id="@+id/chew_toys_list_wrapper"
  39. android:layout_width="match_parent"
  40. android:layout_height="match_parent">
  41. <androidx.recyclerview.widget.RecyclerView
  42. android:id="@+id/chew_toy_list"
  43. android:layout_width="match_parent"
  44. android:layout_height="match_parent"
  45. android:contentDescription="@string/cd_chew_toys_list"
  46. app:layoutManager="LinearLayoutManager"
  47. app:visibleGone="@{!isLoading}" />
  48. </FrameLayout>
  49. </FrameLayout>
  50. </LinearLayout>
  51. </layout>
  52. DogFragment.java
  53. public class DogFragment extends Fragment {
  54. private static final String TAG = "\t\tDogFragment";
  55. private static final String KEY_DOG_ID = "dog_id";
  56. private final ChewToyClickCallback mChewToyClickCallback =
  57. chewToy -> {
  58. // no-op
  59. };
  60. private DogFragmentBinding mBinding;
  61. private ChewToyAdapter mChewToyAdapter;
  62. public static DogFragment forDog(int dogId) {
  63. DogFragment fragment = new DogFragment();
  64. Bundle args = new Bundle();
  65. args.putInt(KEY_DOG_ID, dogId);
  66. fragment.setArguments(args);
  67. return fragment;
  68. }
  69. @Nullable
  70. @Override
  71. public View onCreateView(
  72. @NonNull LayoutInflater inflater,
  73. @Nullable ViewGroup container,
  74. @Nullable Bundle savedInstanceState) {
  75. mBinding = DataBindingUtil.inflate(inflater, R.layout.dog_fragment, container, false);
  76. mChewToyAdapter = new ChewToyAdapter(mChewToyClickCallback);
  77. mBinding.chewToyList.setAdapter(mChewToyAdapter);
  78. return mBinding.getRoot();
  79. }
  80. @Override
  81. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  82. DogViewModel.Factory factory =
  83. new DogViewModel.Factory(
  84. requireActivity().getApplication(), requireArguments().getInt(KEY_DOG_ID));
  85. final DogViewModel model =
  86. new ViewModelProvider(this, factory).get(DogViewModel.class);
  87. mBinding.setLifecycleOwner(getViewLifecycleOwner());
  88. mBinding.setDogViewModel(model);
  89. subscribeToModel(model);
  90. }
  91. private void subscribeToModel(final DogViewModel model) {
  92. model
  93. .getChewToys()
  94. .observe(
  95. getViewLifecycleOwner(),
  96. chewToyEntities -> {
  97. if (chewToyEntities != null) {
  98. mBinding.setIsLoading(false);
  99. mChewToyAdapter.submitList(chewToyEntities);
  100. } else {
  101. mBinding.setIsLoading(true);
  102. }
  103. });
  104. }
  105. @Override
  106. public void onDestroyView() {
  107. mBinding = null;
  108. mChewToyAdapter = null;
  109. super.onDestroyView();
  110. }
  111. }
  112. DogViewModel.java
  113. public class DogViewModel extends AndroidViewModel {
  114. private static final String TAG = "\t\tDogViewModel";
  115. private final LiveData<DogEntity> mObservableDog;
  116. private final LiveData<List<ChewToyEntity>> mObservableChewToys;
  117. public DogViewModel(
  118. @NonNull Application application, DataRepository repository, final int dogId) {
  119. super(application);
  120. mObservableChewToys = repository.loadChewToysById(dogId);
  121. mObservableDog = repository.loadDog(dogId);
  122. }
  123. public LiveData<List<ChewToyEntity>> getChewToys() {
  124. return mObservableChewToys;
  125. }
  126. public LiveData<DogEntity> getDog() {
  127. return mObservableDog;
  128. }
  129. public static class Factory extends ViewModelProvider.NewInstanceFactory {
  130. @NonNull private final Application mApplication;
  131. private final int mDogId;
  132. private final DataRepository mRepository;
  133. public Factory(@NonNull Application application, int dogId) {
  134. mApplication = application;
  135. mDogId = dogId;
  136. mRepository = ((DogApp) application).getRepository();
  137. }
  138. @SuppressWarnings("unchecked")
  139. @Override
  140. @NonNull
  141. public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
  142. return (T) new DogViewModel(mApplication, mRepository, mDogId);
  143. }
  144. }
  145. }
  146. BindingAdapters.java
  147. public class BindingAdapters {
  148. @BindingAdapter("visibleGone")
  149. public static void showHide(View view, boolean show) {
  150. view.setVisibility(show ? View.VISIBLE : View.GONE);
  151. }
  152. }
  153. DogClickCallback.java
  154. public interface DogClickCallback {
  155. void onClick(Dog dog);
  156. }
  157. dao 查询
  158. @Query("select * from dog_table where id = :dogId")
  159. LiveData<DogEntity> loadDog(int dogId);
  160. DogAdapter.java
  161. public class DogAdapter extends RecyclerView.Adapter<DogAdapter.DogViewHolder> {
  162. private static final String TAG = "\t\tDogAdapter";
  163. @Nullable private final DogClickCallback mDogClickCallback;
  164. List<? extends Dog>
  165. <details>
  166. <summary>英文:</summary>
  167. &gt; Cannot find a setter for
  168. &gt; &lt;the_derek.dogstuff.databinding.DogItemBinding app:product&gt; that
  169. &gt; accepts parameter type
  170. &gt; &#39;androidx.lifecycle.LiveData&lt;the_derek.dogstuff.db.entity.DogEntity&gt;&#39;
  171. &gt; If a binding adapter provides the setter, check that the adapter is
  172. &gt; annotated correctly and that the parameter type matches.
  173. Ive been going line-by-line through my app for over two days now. I used the Google provided BasicSample Android Room app to mock up my own app but am getting this error when I include this in my dog_fragment.xml
  174. &lt;include
  175. layout=&quot;@layout/dog_item&quot;
  176. app:product=&quot;@{dogViewModel.dog}&quot; /&gt;
  177. The dog_item layout (dog_item.xml) is for showing a list of dogs, when you then click on it, it will bring you to a dog details screen (dog_fragment.xml). Without it, everything works great but its missing the dog tile to play into the details screen, and will only show a list of chew_toys.
  178. &gt; dog_fragment.xml
  179. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  180. &lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  181. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
  182. &lt;data&gt;
  183. &lt;import type=&quot;android.view.View&quot; /&gt;
  184. &lt;variable
  185. name=&quot;isLoading&quot;
  186. type=&quot;boolean&quot; /&gt;
  187. &lt;variable
  188. name=&quot;dog&quot;
  189. type=&quot;the_derek.dogstuff.viewmodel.DogViewModel&quot; /&gt;
  190. &lt;/data&gt;
  191. &lt;LinearLayout
  192. android:layout_width=&quot;match_parent&quot;
  193. android:layout_height=&quot;match_parent&quot;
  194. android:background=&quot;@color/cardview_light_background&quot;
  195. android:orientation=&quot;vertical&quot;&gt;
  196. &lt;include
  197. layout=&quot;@layout/dog_item&quot;
  198. app:product=&quot;@{dogViewModel.dog}&quot; /&gt;
  199. &lt;FrameLayout
  200. android:layout_width=&quot;match_parent&quot;
  201. android:layout_height=&quot;match_parent&quot;&gt;
  202. &lt;TextView
  203. android:id=&quot;@+id/tv_loading_chew_toys&quot;
  204. android:layout_width=&quot;match_parent&quot;
  205. android:layout_height=&quot;match_parent&quot;
  206. android:text=&quot;@string/loading_chew_toys&quot;
  207. app:visibleGone=&quot;@{isLoading}&quot; /&gt;
  208. &lt;FrameLayout
  209. android:id=&quot;@+id/chew_toys_list_wrapper&quot;
  210. android:layout_width=&quot;match_parent&quot;
  211. android:layout_height=&quot;match_parent&quot;&gt;
  212. &lt;androidx.recyclerview.widget.RecyclerView
  213. android:id=&quot;@+id/chew_toy_list&quot;
  214. android:layout_width=&quot;match_parent&quot;
  215. android:layout_height=&quot;match_parent&quot;
  216. android:contentDescription=&quot;@string/cd_chew_toys_list&quot;
  217. app:layoutManager=&quot;LinearLayoutManager&quot;
  218. app:visibleGone=&quot;@{!isLoading}&quot; /&gt;
  219. &lt;/FrameLayout&gt;
  220. &lt;/FrameLayout&gt;
  221. &lt;/LinearLayout&gt;
  222. &lt;/layout&gt;
  223. &gt; DogFragment.java
  224. public class DogFragment extends Fragment {
  225. private static final String TAG = &quot;\t\tDogFragment&quot;;
  226. private static final String KEY_DOG_ID = &quot;dog_id&quot;;
  227. private final ChewToyClickCallback mChewToyClickCallback =
  228. chewToy -&gt; {
  229. // no-op
  230. };
  231. private DogFragmentBinding mBinding;
  232. private ChewToyAdapter mChewToyAdapter;
  233. public static DogFragment forDog(int dogId) {
  234. DogFragment fragment = new DogFragment();
  235. Bundle args = new Bundle();
  236. args.putInt(KEY_DOG_ID, dogId);
  237. fragment.setArguments(args);
  238. return fragment;
  239. }
  240. @Nullable
  241. @Override
  242. public View onCreateView(
  243. @NonNull LayoutInflater inflater,
  244. @Nullable ViewGroup container,
  245. @Nullable Bundle savedInstanceState) {
  246. mBinding = DataBindingUtil.inflate(inflater, R.layout.dog_fragment, container, false);
  247. mChewToyAdapter = new ChewToyAdapter(mChewToyClickCallback);
  248. mBinding.chewToyList.setAdapter(mChewToyAdapter);
  249. return mBinding.getRoot();
  250. }
  251. @Override
  252. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  253. DogViewModel.Factory factory =
  254. new DogViewModel.Factory(
  255. requireActivity().getApplication(), requireArguments().getInt(KEY_DOG_ID));
  256. final DogViewModel model =
  257. new ViewModelProvider(this, factory).get(DogViewModel.class);
  258. mBinding.setLifecycleOwner(getViewLifecycleOwner());
  259. mBinding.setDogViewModel(model);
  260. subscribeToModel(model);
  261. }
  262. private void subscribeToModel(final DogViewModel model) {
  263. model
  264. .getChewToys()
  265. .observe(
  266. getViewLifecycleOwner(),
  267. chewToyEntities -&gt; {
  268. if (chewToyEntities != null) {
  269. mBinding.setIsLoading(false);
  270. mChewToyAdapter.submitList(chewToyEntities);
  271. } else {
  272. mBinding.setIsLoading(true);
  273. }
  274. });
  275. }
  276. @Override
  277. public void onDestroyView() {
  278. mBinding = null;
  279. mChewToyAdapter = null;
  280. super.onDestroyView();
  281. }
  282. }
  283. &gt; DogViewModel.java
  284. public class DogViewModel extends AndroidViewModel {
  285. private static final String TAG = &quot;\t\tDogViewModel&quot;;
  286. private final LiveData&lt;DogEntity&gt; mObservableDog;
  287. private final LiveData&lt;List&lt;ChewToyEntity&gt;&gt; mObservableChewToys;
  288. public DogViewModel(
  289. @NonNull Application application, DataRepository repository, final int dogId) {
  290. super(application);
  291. mObservableChewToys = repository.loadChewToysById(dogId);
  292. mObservableDog = repository.loadDog(dogId);
  293. }
  294. public LiveData&lt;List&lt;ChewToyEntity&gt;&gt; getChewToys() {
  295. return mObservableChewToys;
  296. }
  297. public LiveData&lt;DogEntity&gt; getDog() {
  298. return mObservableDog;
  299. }
  300. public static class Factory extends ViewModelProvider.NewInstanceFactory {
  301. @NonNull private final Application mApplication;
  302. private final int mDogId;
  303. private final DataRepository mRepository;
  304. public Factory(@NonNull Application application, int dogId) {
  305. mApplication = application;
  306. mDogId = dogId;
  307. mRepository = ((DogApp) application).getRepository();
  308. }
  309. @SuppressWarnings(&quot;unchecked&quot;)
  310. @Override
  311. @NonNull
  312. public &lt;T extends ViewModel&gt; T create(@NonNull Class&lt;T&gt; modelClass) {
  313. return (T) new DogViewModel(mApplication, mRepository, mDogId);
  314. }
  315. }
  316. }
  317. &gt; BindingAdapters.java
  318. public class BindingAdapters {
  319. @BindingAdapter(&quot;visibleGone&quot;)
  320. public static void showHide(View view, boolean show) {
  321. view.setVisibility(show ? View.VISIBLE : View.GONE);
  322. }
  323. }
  324. &gt; DogClickCallback.java
  325. public interface DogClickCallback {
  326. void onClick(Dog dog);
  327. }
  328. &gt; dao query
  329. @Query(&quot;select * from dog_table where id = :dogId&quot;)
  330. LiveData&lt;DogEntity&gt; loadDog(int dogId);
  331. &gt; DogAdapter.java
  332. public class DogAdapter extends RecyclerView.Adapter&lt;DogAdapter.DogViewHolder&gt; {
  333. private static final String TAG = &quot;\t\tDogAdapter&quot;;
  334. @Nullable private final DogClickCallback mDogClickCallback;
  335. List&lt;? extends Dog&gt; mDogList;
  336. public DogAdapter(@Nullable DogClickCallback clickCallback) {
  337. Log.i(TAG, &quot;DogAdapter: public constructor&quot;);
  338. mDogClickCallback = clickCallback;
  339. setHasStableIds(true);
  340. }
  341. public void setDogList(final List&lt;? extends Dog&gt; dogList) {
  342. if (mDogList == null) {
  343. mDogList = dogList;
  344. notifyItemRangeInserted(0, dogList.size());
  345. } else {
  346. DiffUtil.DiffResult result =
  347. DiffUtil.calculateDiff(
  348. new DiffUtil.Callback() {
  349. @Override
  350. public int getOldListSize() {
  351. return mDogList.size();
  352. }
  353. @Override
  354. public int getNewListSize() {
  355. return dogList.size();
  356. }
  357. @Override
  358. public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
  359. return mDogList.get(oldItemPosition).getId()
  360. == dogList.get(newItemPosition).getId();
  361. }
  362. @Override
  363. public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
  364. Dog newDog = dogList.get(newItemPosition);
  365. Dog oldDog = mDogList.get(oldItemPosition);
  366. return newDog.getId() == oldDog.getId()
  367. &amp;&amp; TextUtils.equals(newDog.getName(), oldDog.getName());
  368. }
  369. });
  370. mDogList = dogList;
  371. result.dispatchUpdatesTo(this);
  372. }
  373. }
  374. @Override
  375. @NonNull
  376. public DogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  377. DogItemBinding binding =
  378. DataBindingUtil.inflate(
  379. LayoutInflater.from(parent.getContext()), R.layout.dog_item, parent, false);
  380. binding.setCallback(mDogClickCallback);
  381. return new DogViewHolder(binding);
  382. }
  383. @Override
  384. public void onBindViewHolder(@NonNull DogViewHolder holder, int position) {
  385. holder.binding.setDog(mDogList.get(position));
  386. holder.binding.executePendingBindings();
  387. }
  388. @Override
  389. public int getItemCount() {
  390. return mDogList == null ? 0 : mDogList.size();
  391. }
  392. @Override
  393. public long getItemId(int position) {
  394. return mDogList.get(position).getId();
  395. }
  396. static class DogViewHolder extends RecyclerView.ViewHolder {
  397. final DogItemBinding binding;
  398. public DogViewHolder(DogItemBinding binding) {
  399. super(binding.getRoot());
  400. this.binding = binding;
  401. }
  402. }
  403. }
  404. ----------
  405. ----------
  406. (DogEntity also has Dog model class, if that helps)
  407. I&#39;ve tried Invalidate Caches/Restart, I&#39;ve tried Clean Project, Rebuild Project. I&#39;ve started a new project and copied my files into it.
  408. Ooh, also, this is an error in addition:
  409. import the_derek.dogstuff.databinding.DogFragmentBindingImpl;
  410. It tells me it cannot resolve **DogFragmentBindingImpl**
  411. I don&#39;t know how it&#39;s not getting generated but I assume the problems are intertwined. I don&#39;t know if I missed putting any code that could help, please let me know.
  412. (modeled after)
  413. [android architecture-components-samples][1]
  414. [1]: https://github.com/android/architecture-components-samples/tree/master/BasicSample
  415. </details>
  416. # 答案1
  417. **得分**: 1
  418. 这是[绑定适配器][1]错误。如果您在XML中编写了'app:product',则在您的KotlinJava类之一中必须有一个名为'product'的绑定适配器。例如,对于您的
  419. &gt; app:product="@{dogViewModel.dog}"
  420. 需要有类似于以下内容的代码:

@BindingAdapter("product")
fun yourFunctionName(yourViewType: YourViewType, data: List?) {
// 在此处放置您的绑定代码
}

  1. 了解有关数据绑定和绑定适配器的更多信息。
  2. [1]: https://developer.android.com/topic/libraries/data-binding/binding-adapters
  3. <details>
  4. <summary>英文:</summary>
  5. This is [binding adapter][1] error. If you write &#39;app:product&#39; in your XML, there has to be binding adapter inside one of your kotlin or java classes with the name of &#39;product&#39;. For example, for your
  6. &gt; app:product=&quot;@{dogViewModel.dog}&quot;
  7. there needs to be something like this:

@BindingAdapter("product")
fun yourFunctionName(yourViewType: YourViewType, data: List<DogEntity>?) {
// your binding code here
}

  1. Read more about data binding and binding adapters.
  2. [1]: https://developer.android.com/topic/libraries/data-binding/binding-adapters
  3. </details>
  4. # 答案2
  5. **得分**: 1
  6. [德里克·福西特的解决方案](https://stackoverflow.com/a/63601993) 告诉你,在 include 标签内部,参数名必须与所包含布局内使用的变量名匹配。
  7. ```xml
  8. &lt;include
  9. layout=&quot;@layout/dog_item&quot;
  10. app:dog=&quot;@{dogViewModel.dog}&quot; /&gt;

dog_item

  1. &lt;data&gt;
  2. &lt;variable
  3. name=&quot;dog&quot;
  4. type=&quot;$path.DogClass&quot; /&gt;
  5. &lt;/data&gt;

在绑定方面,没有通用的参数名称与变量匹配,您可以自己定义名称,然后相应地进行设置。

英文:

the derek Fawcett's solution is telling you that the parameter name inside the include-tag has to match the variable name used inside the included layout.

  1. &lt;include
  2. layout=&quot;@layout/dog_item&quot;
  3. app:dog=&quot;@{dogViewModel.dog}&quot; /&gt;

dog_item

  1. &lt;data&gt;
  2. &lt;variable
  3. name=&quot;dog&quot;
  4. type=&quot;$path.DogClass&quot; /&gt;
  5. &lt;/data&gt;

There are no general parameter names to match variables when it comes to bindings, you can define the names yourself and then set them accordingly.

答案3

得分: -3

我花了几个星期或一个月的时间,认为“app:product”是一种用于XML的标准短语或约定。我以为“product”是一个类似于“gravity”或“layout”的通用术语...如果你明白我的意思的话。因为当我发表这个问题时,我对Android还不熟悉,我从来没有想过“app:”后面的术语需要根据数据中的变量进行更改。

英文:

I spent weeks or a month thinking that "app:product" was some sort of standard phrase or convention for XML. I thought "product" was a generic term used like "gravity" or "layout"..... if you get what I'm saying. Because when I posted the question I was new to Android, I never thought that the term after "app:" needed to be changed depending on the variable in data.

  1. &lt;include
  2. layout=&quot;@layout/dog_item&quot;
  3. app:dog=&quot;@{dogViewModel.dog}&quot; /&gt;

答案4

得分: -4

你在这里传递了一个 LiveData 对象:

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

你需要传递:

  1. app:product="@{dogViewModel.dog.value}"
英文:

You're passing in a LiveData object here:

  1. app:product=&quot;@{dogViewModel.dog}&quot;

You need to pass in:

  1. app:product=&quot;@{dogViewModel.dog.value}&quot;

huangapple
  • 本文由 发表于 2020年8月22日 02:42:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63528445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定