3

我正在尝试实现新的 android 架构组件,并在片段和视图模型中使用了实时数据,但是当我向实时数据添加观察者时,应用程序崩溃并抛出此异常。

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

列表片段:

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);
        }
    }
}

视图模型:

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;
    } 
}

注意数据库:

@Database(entities = {NoteEntity.class}, version = 1)
public abstract class NoteDatabase extends RoomDatabase {
    public abstract NoteDao getNoteDao();
}

如果在实时数据上添加正面,应用程序会崩溃。

我正在使用“Room.databaseBuilder(....)”函数在我的应用程序类中构建数据库的单个实例,并在任何地方使用它,我的 NoteEntity 类有三个字段,一个是 id,它是自动生成的主键。

4

2 回答 2

5

我有类似的错误,在我的情况下是由 gradle.build 文件中的这种依赖关系引起的:

implementation "android.arch.lifecycle:common-java8:1.0.0-beta2"
于 2017-10-11T12:50:46.520 回答
0

FirebaseUI 尚未将 android.arch.lifecycle 更新为 1.0.0-beta2。

使用 1.0.0-beta1 而不是 1.0.0-beta2。
等到他们更新生命周期库。

于 2017-10-13T03:36:05.890 回答