1

我正在编写一个程序,其中有两个 LiveData 对象,如图所示:


    public LiveData<placeImages> chosenImages;
    public LiveData<List<Bitmap>> chosenBitmaps= Transformations.map(chosenImages, (placeimags) ->
    {
        ArrayList<Bitmap> bitmaps=new ArrayList<Bitmap>();
        for(PhotoI photoI:placeimags.images)
        {
            try {
                Bitmap b=getThumbnail(app.getContentResolver(),photoI.imageURI);
                if(b!=null) {
                    bitmaps.add(b);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return bitmaps;
    });

问题是每当我观察到selectedImages LiveData它工作正常,但是当我观察到selectedBitmaps LiveData它返回以下错误:

2020-07-18 17:40:39.790 32164-32164/com.nisarg.locsav E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nisarg.locsav, PID: 32164
    java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.lifecycle.LiveData.observeForever(androidx.lifecycle.Observer)' on a null object reference
        at androidx.lifecycle.MediatorLiveData$Source.plug(MediatorLiveData.java:141)
        at androidx.lifecycle.MediatorLiveData.onActive(MediatorLiveData.java:118)
        at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:437)
        at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:395)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:300)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
        at androidx.fragment.app.FragmentViewLifecycleOwner.handleLifecycleEvent(FragmentViewLifecycleOwner.java:51)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:2737)
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:365)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1194)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6635)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

我认为 Transformations.map() 返回空值。我之前在一个项目中做过类似的事情,当我使用 Transformations.map() 时也发生了同样的事情。有谁知道为什么会这样?

4

2 回答 2

0

更改 public LiveData<placeImages> chosenImages;

public LiveData<placeImages> chosenImages = MutableLiveData()

于 2021-02-23T18:51:05.510 回答
0

想必MutableLiveData后面有什么地方chosenImages

您所描述的情况发生在MutableLiveData未使用noargs构造函数初始化时new MutableLiveData<placeImages>()Transformations.map()派生的LiveData也未初始化,因此null.

解决方案是初始化MutableLiveDataon 创建,即使它被初始化为 null:new MutableLiveData<placeImages>(null)以便派生也被初始化。

于 2021-12-24T17:31:58.153 回答