Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用房间数据库检索单行数据_Java_Android_Nullpointerexception_Android Room_Android Mvvm - Fatal编程技术网

Java 无法使用房间数据库检索单行数据

Java 无法使用房间数据库检索单行数据,java,android,nullpointerexception,android-room,android-mvvm,Java,Android,Nullpointerexception,Android Room,Android Mvvm,无法从获取此输出的数据库中获取特定记录 我的输出是: java.lang.RuntimeException:无法在空对象引用上启动活动组件信息{appwork.com.example/appwork.com.example.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.arch.lifecycle.LiveData.observe(android.arch.lifecycle.LifecycleOwner,a

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

我的输出是:

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

我使用room数据库、Modelview、Repository和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);
    }

公共类MainActivity扩展了AppCompatActivity{
私有NoteViewModel NoteViewModel;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noteViewModel=
ViewModelProviders.of(this.get)(NoteViewModel.class);
noteViewModel.getNote(“test7”).observe(这是一个新的观察者()
{
@凌驾
更改后的公共void(@Nullable Note)
{
Toast.makeText(MainActivity.this,
找到的标题为:“+note.getTitle(),
Toast.LENGTH_LONG).show();
}
});
}
}   
公共类NoteViewModel扩展了AndroidViewModel
{
私人实时数据说明;
public NoteViewModel(@NonNull应用程序)
{
超级(应用);
//note=repository.getNote(“”);
}
公共LiveData getNote(字符串搜索)
{
getNote(搜索);
如果(注意!=null)
{
退货单;
}
返回null;
}
}
公共类记事本存储库{
私人记事本;
私人实时数据说明;
公共记事本存储库(应用程序)
{
NoteDatabase NoteDatabase=
NoteDatabase.getInstance(应用程序);
noteDao=noteDatabase.noteDao();
}
公共LiveData getNote(字符串搜索)
{
新建SearchNoteAsyncTask(noteDao)。执行(搜索);
如果(注意!=null)
{
退货单;
}
返回null;
}

公共静态void asyncFinished(LiveData结果) { 注=结果; } 公共静态类SearchNoteAsyncTask扩展了AsyncTask { 私人记事本; 私人实时数据说明; 私有SearchNoteAsyncTask(NoteDao NoteDao) { this.noteDao=noteDao; } 公共LiveData doInBackground(字符串…搜索) { return noteDao.getNote(搜索[0]); } public void onPostExecute(LiveData结果) { 完成(结果); } } } @刀 公共接口NoteDao{ @查询(“从注释表中选择*,其中标题=:按优先级描述的搜索顺序”) LiveData getNote(字符串搜索); }
我在存储库调用中收到响应,但无法从public获取值

public LiveData<Note> getNote(String search){
    repository.getNote(search);
    if(note != null)
    {
        return note; // this is the livedata reference that UI is observing
    }
    return null;
}
静态类SearchNoteAsyncTask扩展了AsyncTask
任何工作的例子都将是伟大的


谢谢

问题在于,您正在将不同的Livedata引用返回到中的视图-

public static void asyncFinished(LiveData<Note> results) {
    note = results;
}
public-LiveData-getNote(字符串搜索){
getNote(搜索);
如果(注意!=null)
{
return note;//这是UI正在观察的livedata引用
}
返回null;
}
当您从repo获得响应时,您会更改livedata的引用-

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

publicstaticvoid异步完成(LiveData结果){
注=结果;
}
但您的UI正在侦听以前的LiveData

您应该更改原始note livedata中的值,而不是更改引用

那看起来像-

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

publicstaticvoid异步完成(LiveData结果){
note.setValue(results.getValue);//虽然不是首选方法,但它将帮助您理解当前实现无法工作的原因。
}
最好是在原始livedata上使用转换-

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.
}
public-LiveData-getNote(字符串搜索){
返回Transformations.map(repository.getNote(search),
值->注释.设置值(值));
}

Hi Manoj,欢迎来到SO。这是一个NullPointerException,请参考公共静态void asyncFinished(LiveData结果){note.setValue(results.getValue);//虽然不是首选方法,但它将帮助您理解当前实现无法工作的原因。}我没有在我的笔记中创建setValue方法classnote是livedata对象的引用-
私有livedata笔记我是否应该在注释中设置getValue和setValue类setValue是LiveData提供的方法。