Тег Android – view неверен в представлении: null (CursorAdapter и DataBinding)

Поэтому вчера я, наконец, нашел время, чтобы попробовать новую библиотеку DataBinding. Я попытался использовать его с CursorAdapter, но без успеха.

Поскольку Androidguys написаны на их странице:

Если вы используете элементы привязки данных внутри адаптера ListView или RecyclerView, вы можете использовать:

ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false); //or ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false); 

Но в CursorAdapter я должен разделить Inflating и Binding на 2 метода. Итак, как правильно его реализовать? В основном у меня есть тег Exception: view неверен в представлении: null

 private class BookCursorAdapter extends CursorAdapter { public BookCursorAdapter(Context context) { super(context, null, 0); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { BookListItemBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.book_list_item, parent, false); return binding.getRoot(); // or just // return getLayoutInflater().inflate(R.layout.book_list_item, parent, false); } @Override public void bindView(View view, Context context, Cursor cursor) { // here it fails (and i cannot see source code of bind method) BookListItemBinding.bind(view).setBook(new Book(cursor)); } } 

Как правильно реализовать привязку к CursorAdapter?

10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: FATAL EXCEPTION: main 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: Процесс: kandrac. Xyz.library, PID: 6231 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: java.lang.RuntimeException: тег просмотра неверен в представлении: null 10-14 06:25 : 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at kandrac.xyz.library.databinding.BookListItemBinding.bind (BookListItemBinding.java:135) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz. Библиотека E / AndroidRuntime: at kandrac.xyz.library.MainActivity $ BookCursorAdapter.bindView (MainActivity.java:95) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.widget. CursorAdapter.getView (CursorAdapter.java:254) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.widget.AbsListView.obtainView (AbsListView.java:2347) 10-14 06 : 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.widget.ListView.makeAndA DdView (ListView.java:1864) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.widget.ListView.fillDown (ListView.java:698) 10-14 06:25 : 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.widget.ListView.correctTooLow (ListView.java:1474) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime : At android.widget.ListView.fillGap (ListView.java:672) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.widget.AbsListView.trackMotionScroll (AbsListView.java: 4991) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.widget.AbsListView.scrollIfNeeded (AbsListView.java:3418) 10-14 06: 25: 43.847 6231-6231 / Kandrac.xyz.library E / AndroidRuntime: at android.widget.AbsListView.onTouchMove (AbsListView.java:3801) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.widget. AbsListView.onTouchEvent (AbsListView.java:3632) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntim E: at android.view.View.dispatchTouchEvent (View.java:8471) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java : 2399) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2092) 10-14 06: 25: 43.847 6231-6231 /kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2405) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view .ViewGroup.dispatchTouchEvent (ViewGroup.java:2106) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2405) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2106) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTransform EdTouchEvent (ViewGroup.java:2405) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2106) 10-14 06:25 : 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2405) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime : At android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2106) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java: 2405) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2106) 10-14 06: 25: 43.847 6231-6231 / Kandrac.xyz.library E / AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2405) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view. ViewGroup.dispatchTouchEvent (ViewGroup.java:2106) 10 -14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent (PhoneWindow.java:2369) 10-14 06:25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent (PhoneWindow.java:1719) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz .library E / AndroidRuntime: at android.app.Activity.dispatchTouchEvent (Activity.java:2742) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.support.v7.internal .view.WindowCallbackWrapper.dispatchTouchEvent (WindowCallbackWrapper.java:60) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent (PhoneWindow.java:2330) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.View.dispatchPointerEvent (View.java:8666) 10-14 06:25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: при Ndroid.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent (ViewRootImpl.java:4123) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess (ViewRootImpl. Java: 3989) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ InputStage.deliver (ViewRootImpl.java:3544) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ InputStage.onDeliverToNext (ViewRootImpl.java:3597) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime : At android.view.ViewRootImpl $ InputStage.forward (ViewRootImpl.java:3563) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ AsyncInputStage.forward ( ViewRootImpl.java:3680) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ InputStage.apply (ViewRootImpl.java:3571) 10-14 06:25 : 43,847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ AsyncInputStage.apply (ViewRootImpl.java:3737) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ InputStage.deliver (ViewRootImpl.java:3544) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ InputStage.onDeliverToNext (ViewRootImpl.java:3597) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ InputStage.forward (ViewRootImpl.java:3563) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ InputStage.apply (ViewRootImpl.java:3571) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ InputStage .deliver (ViewRootImpl.java:3544) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl.deliverInputEvent (ViewRootImpl.java:5807) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at Android.view.ViewRootImpl.doProcessInputEvents (ViewRootImpl.java:5781) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl.enqueueInputEvent (ViewRootImpl.java:5752) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent (ViewRootImpl.java:5897) 10-14 06: 25: 43.847 6231-6231 / Kandrac.xyz.library E / AndroidRuntime: at android.view.InputEventReceiver.dispatchInputEvent (InputEventReceiver.java:185) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view. InputEventReceiver.nativeConsumeBatchedInputEvents (собственный метод) 10-14 06: 25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime: at android.view.InputEven

Solutions Collecting From Web of "Тег Android – view неверен в представлении: null (CursorAdapter и DataBinding)"

На основе видео разработчиков Android о привязке данных. Похоже, что ItemBinding устанавливается как тег для представления (в их случае он передается внутри ViewHolder) и извлекается позже. Вот где я нашел идею найти привязку внутри просмотров:

Таким образом, решение:

 private class BookCursorAdapter extends CursorAdapter { ... @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { BookListItemBinding binding = BookListItemBinding.inflate(getLayoutInflater(), parent, false); return binding.getRoot(); } @Override public void bindView(View view, Context context, Cursor cursor) { Book book = new Book(cursor); BookListItemBinding binding = DataBindingUtil.getBinding(view); binding.setBook(book); } }