Android:使用MVVM模式架构的异常观察

时间:2018-08-17 19:28:10

标签: android mvvm android-mvvm

我正在尝试运行采用MVVM模式架构的应用程序,但是却遇到了我无法解决的异常。 我有MainActivity.java,其中包含MainViewModel.java中的firstTime()方法。

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

}

每次运行时,都会收到以下无法解决的异常。

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)

我该如何解决?

1 个答案:

答案 0 :(得分:0)

获得NPE的原因是您错过了在onCreate活动中初始化 viewModel 对象的方法。

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