I am trying to introduce new components of the Android architecture and use the current data in the fragment and view model, but when I add an observer to the current data, the application crashes from this exception.
Process: com.nrs.nsnik.architecturecomponents, PID: 3071 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nrs.nsnik.architecturecomponents/com.nrs.nsnik.architecturec omponents.view.MainActivity}: java.lang.ClassCastException: android.arch.lifecycle.LiveData_LifecycleBoundObserver_LifecycleAdapter cannot be cast to android.arch.lifecycle.GeneratedAdapter . . . . Caused by: java.lang.ClassCastException: android.arch.lifecycle.LiveData_LifecycleBoundObserver_LifecycleAdapter cannot be cast to android.arch.lifecycle.GeneratedAdapter
List snippet:
public class ListFragment extends Fragment { @BindView(R.id.listFragmentRecyclerView) RecyclerView mRecyclerView; @BindView(R.id.listFragmentAddItem) FloatingActionButton mFloatingActionButton; private Unbinder mUnbinder; private CompositeDisposable mCompositeDisposable; private ListViewModel mListViewModel; private List<NoteEntity> mNoteEntityList; private ListAdapter mListAdapter; private NoteDatabase mNoteDatabase; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_list, container, false); mUnbinder = ButterKnife.bind(this, v); mListViewModel = ViewModelProviders.of(this).get(ListViewModel.class); mNoteDatabase = ((MyApplication)getActivity().getApplication()).getNoteDatabaseInstance(); initialize(); listeners(); return v; } private void initialize() { mCompositeDisposable = new CompositeDisposable(); mNoteEntityList = new ArrayList<>(); mListAdapter = new ListAdapter(getActivity(), mNoteEntityList); mListViewModel.getNoteList().observe(this, noteEntityList -> { mListAdapter.swapList(noteEntityList); mListAdapter.notifyDataSetChanged(); }); } private void cleanUp() { if (mUnbinder != null) { mUnbinder.unbind(); } if (mCompositeDisposable != null) { mCompositeDisposable.dispose(); } } private void listeners() { RxView.clicks(mFloatingActionButton).subscribe(o -> { AlertDialog.Builder newNoteDialog = new AlertDialog.Builder(getActivity()); View v = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_add_note_dialog, null); newNoteDialog.setView(v); EditText editText = v.findViewById(R.id.addNoteEditText); newNoteDialog.setNegativeButton(getActivity().getResources().getString(R.string.cancel), (dialogInterface, i) -> { }).setPositiveButton(getActivity().getResources().getString(R.string.add), (dialogInterface, i) -> { if (isValid(editText)) { NoteEntity entity = new NoteEntity(); entity.setNote(editText.getText().toString()); entity.setDate(getCurrentDate()); mNoteDatabase.getNoteDao().insertNote(entity); } }); newNoteDialog.create().show(); }); } private Date getCurrentDate() { Date date = new Date(Calendar.getInstance().getTimeInMillis()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH); simpleDateFormat.format(date); return date; } private boolean isValid(EditText editText) { return !(editText.getText().toString().length() <= 0 || editText.getText().toString().isEmpty()); } @Override public void onDestroy() { super.onDestroy(); cleanUp(); if (BuildConfig.DEBUG) { RefWatcher refWatcher = MyApplication.getRefWatcher(getActivity()); refWatcher.watch(this); } } }
ViewModel:
public class ListViewModel extends AndroidViewModel { private LiveData<List<NoteEntity>> mNoteList; private final NoteDatabase mNoteDatabase; ListViewModel(Application application) { super(application); mNoteDatabase = ((MyApplication)application).getNoteDatabaseInstance(); mNoteList = mNoteDatabase.getNoteDao().getNoteList(); } public LiveData<List<NoteEntity>> getNoteList() { return mNoteList; } }
NoteDatabase:
@Database(entities = {NoteEntity.class}, version = 1) public abstract class NoteDatabase extends RoomDatabase { public abstract NoteDao getNoteDao(); }
The application crashes if you add the current data to the obverse.
I create one database instance in my application class using the "Room.databaseBuilder (....)" function and use it everywhere, and my NoteEntity class has three fields: one is the identifier, which is the primary key, which automatically - generates.