简体   繁体   中英

Android: Exception using MVVM pattern architecture observe

I'm trying to run an app adopting MVVM pattern architecture, but I'm getting an exception that I can't solve. I have my MainActivity.java which has an observe to the firstTime() method in the MainViewModel.java.

MainActivity.java

(...)
    ViewModelProvider.Factory mViewModelFactory;
    ActivityMainBinding mActivityStartBinding;
    private MainViewModel mainViewModel;   

       @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivityStartBinding = getViewDataBinding();
        mainViewModel.firstTime().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                if(aBoolean !=null && aBoolean){
                    mainViewModel.setisFirstRun();
                }

            }
        });

MainViewModel.java

(...)

private final DataManager mDataManager; <--- repository

public LiveData<Boolean> firstTime(){
  if(mDataManager.isFirstRun())
  {
    Timber.d("-------------- Is first read from database ");
    return mDataManager.saveValues();
 }
 else
 {
    Timber.d("-------------- Is not first read from database ");
    return null;
}

}

And every time I run I get the following Exception that I can't resolve.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rd.ch/com.rd.ch.ui.main.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.arch.lifecycle.LiveData.observe(android.arch.lifecycle.LifecycleOwner, android.arch.lifecycle.Observer)' on a null object reference
                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:148)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.arch.lifecycle.LiveData.observe(android.arch.lifecycle.LifecycleOwner, android.arch.lifecycle.Observer)' on a null object reference
                                                               at com.rd.ch.ui.main.MainActivity.onCreate(MainActivity.java:55)
                                                               at android.app.Activity.performCreate(Activity.java:6251)

How can I solve this?

The reason of getting NPE is that you have missed to initialize your viewModel object in activity onCreate.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
    // Rest of the code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM