无法使用会议室数据库检索单行数据

时间:2019-07-12 07:01:21

标签: java android nullpointerexception android-room android-mvvm

无法从数据库中获取特定记录以获取此输出。

我的输出是:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {appwork.com.example / appwork.com.example.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.arch.lifecycle.LiveData .observe(android.arch.lifecycle.LifecycleOwner,android.arch.lifecycle.Observer)”上的空对象引用

我正在使用会议室数据库,Modelview,存储库和Dao文件获取实时数据,但无法通过以下操作从数据库获取特定记录

    <pre><code>
    public class MainActivity extends AppCompatActivity {

        private NoteViewModel noteViewModel;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            noteViewModel = 
    ViewModelProviders.of(this).get(NoteViewModel.class);
            noteViewModel.getNote("test7").observe(this, new Observer<Note>()
            {
                   @Override
                   public void onChanged(@Nullable Note note)
                   {
                       Toast.makeText(MainActivity.this,
                            "found title is : " +note.getTitle(),
                            Toast.LENGTH_LONG).show();
                   }
                 });
            }
        }   

    public class NoteViewModel extends AndroidViewModel
    {
        private LiveData<Note> note;

        public NoteViewModel(@NonNull Application application)
        {
            super(application);
    //        note = repository.getNote("");
        }

        public LiveData<Note> getNote(String search)
        {
            repository.getNote(search);
            if(note != null)
            {
                return note;
            }
            return null;
        }
    }


    public class NoteRepository {

        private NoteDao noteDao;
        private LiveData<Note> note;

        public NoteRepository(Application application)
        {
            NoteDatabase noteDatabase = 
    NoteDatabase.getInstance(application);
            noteDao = noteDatabase.noteDao();
        }

        public LiveData<Note> getNote(String search)
        {
            new SearchNoteAsyncTask(noteDao).execute(search);
            if(note != null)
            {
                return note;
            }
            return null;
        }


       public static void asyncFinished(LiveData<Note> results)
       {
            note = results;
       }

        public static class SearchNoteAsyncTask extends AsyncTask<String, Void, LiveData<Note>>
        {
            private NoteDao noteDao;
            private LiveData<Note> note;

            private SearchNoteAsyncTask(NoteDao noteDao)
            {
                this.noteDao = noteDao;
            } 

            public LiveData<Note> doInBackground(String... search)
            {
                return noteDao.getNote(search[0]);
            }

            public void onPostExecute(LiveData<Note> result)
            {
                asyncFinished(result);            
            }
        }
    }

    @Dao
    public interface NoteDao{
    @Query("Select * from note_table where title =:search Order By priority DESC")
        LiveData<Note> getNote(String search);
    }

我在存储库调用中得到响应,但无法从公共获取价值

static class SearchNoteAsyncTask extends AsyncTask<String, Void, LiveData<Note>>

任何可行的例子都很棒!

谢谢

1 个答案:

答案 0 :(得分:0)

问题是,您正在向-中的视图返回Livedata的不同引用-

public static void asyncFinished(LiveData<Note> results) {
    note = results;
}

当您从回购中获得响应时,您将更改实时数据的引用-

public static void asyncFinished(LiveData<Note> results) {
    note.setValue(results.getValue); // not the preferred way though, but it will help you understand why your current implementation doesn't work.
}

但是您的UI正在监听以前的LiveData。

您应该更改原始注释实时数据中的值,而不是更改参考。

这看起来像-

public LiveData<Note> getNote(String search){
   return Transformations.map(repository.getNote(search), 
          value -> note.setValue(value));  
}

最好是在原始实时数据上使用Transformation-

{{1}}