我正在开发一个输入表单应用程序,它具有一个主菜单,一个数据输入表单和一个显示表单,以显示添加到数据库中的所有项目,我正在使用android Room库。
当我按下主菜单中的“查看塔”按钮时,我的应用程序崩溃了,但是只有在加载应用程序后首先按下它时,我的应用程序才会崩溃。我认为这与我将字数据库值设置为NonNull有关,但是我需要这样做以避免将空值添加到表中。我已经厌倦了调试它,但似乎找不到与之相关的错误,调试器可以正常运行,直到到达“ super.onResume();”为止。行暂停一秒钟,然后应用崩溃。
任何帮助将不胜感激。
谢谢
我的onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.RecyclerView);
final WordListAdapter adapter = new WordListAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mWordViewModel = ViewModelProviders.of(this).get(WordViewModel.class);
try {
mWordViewModel.getAllWords().observe(this, new Observer<List<Word>>() {
@Override
public void onChanged(List<Word> words) {
try {
adapter.setWords(words);
} catch (Exception e) {
Log.d("SET WORDS FAIL", e.toString());
}
}
});
} catch (Exception e) {
Log.d("GET ALL WORD FAIL", e.toString());
}
我的onResume方法:
@Override
protected void onResume() {
super.onResume();
try {
Word word = new Word(getIntent().getStringExtra(EXTRA_REPLY));
mWordViewModel.insert(word);
} catch (Exception e) {
Log.d("ON REUSME FAIL", e.toString());
}
}
My ViewModel:
public class WordViewModel extends AndroidViewModel {
private WordRepository repository;
private LiveData<List<Word>> allWords;
public WordViewModel (Application application) {
super(application);
repository = new WordRepository(application);
allWords = repository.getAllWords();
}
LiveData<List<Word>> getAllWords() {
return allWords;
}
public void insert(Word word) {
repository.insert(word);
}
}
我的回忆录:
public class WordRepository {
private WordDAO mWordDao;
private LiveData<List<Word>> allWords;
WordRepository(Application application) {
WordRoomDataBase db = WordRoomDataBase.getDataBase(application);
mWordDao = db.wordDAO();
allWords = db.wordDAO().getAllWords();
}
LiveData<List<Word>> getAllWords() {
return allWords;
}
public void insert (Word word) {
new insertAsyncTask(mWordDao).execute(word);
}
private static class insertAsyncTask extends AsyncTask<Word, Void, Void> {
private WordDAO mAsyncTaskDAO;
insertAsyncTask(WordDAO dao) {
mAsyncTaskDAO = dao;
}
@Override
protected Void doInBackground(final Word... words) {
mAsyncTaskDAO.insert(words[0]);
return null;
}
}
}
我的数据访问对象:
@Dao
public interface WordDAO {
@Insert
void insert(Word word);
@Query("DELETE FROM word_table")
void deleteAll();
@Query("SELECT * from word_table ORDER BY word ASC")
LiveData<List<Word>> getAllWords();
}
我的对象:
@Entity(tableName = "word_table")
public class Word {
@ColumnInfo(name = "word")
@NonNull
@PrimaryKey
private String Word;
public Word(String Word) {
this.Word = Word;
}
public String getWord() {
return this.Word;
}
}
我的RecyclerView适配器:
public class WordListAdapter extends RecyclerView.Adapter<WordListAdapter.WordViewHolder> {
class WordViewHolder extends RecyclerView.ViewHolder {
private final TextView wordItemView;
private WordViewHolder(View itemView) {
super(itemView);
wordItemView = itemView.findViewById(R.id.RecyclerViewTextView);
}
}
private final LayoutInflater mInflator;
private List<Word> mWords;
WordListAdapter(Context context) {
mInflator = LayoutInflater.from(context);
}
@NonNull
@Override
public WordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = mInflator.inflate(R.layout.recycler_view_item, parent, false);
return new WordViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull WordViewHolder holder, int position) {
if (mWords != null) {
Word current = mWords.get(position);
holder.wordItemView.setText(current.getWord());
} else {
holder.wordItemView.setText("No Words");
}
}
@Override
public int getItemCount() {
if (mWords != null) {
return mWords.size();
} else return 0;
}
void setWords(List<Word> words) {
mWords = words;
notifyDataSetChanged();
}
}