尝试在空对象引用上调用接口方法“java.lang.Object java.util.Map$Entry.getValue()”

Posted

技术标签:

【中文标题】尝试在空对象引用上调用接口方法“java.lang.Object java.util.Map$Entry.getValue()”【英文标题】:Attempt to invoke interface method 'java.lang.Object java.util.Map$Entry.getValue()' on a null object reference 【发布时间】:2019-03-01 00:19:47 【问题描述】:

我在我的应用程序中遇到以下问题,不知道它是如何产生或解决的。任何帮助将不胜感激。

Caused by java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map$Entry.getValue()' on a null object reference
   at android.arch.lifecycle.LifecycleRegistry.isSynced(LifecycleRegistry.java:146)
   at android.arch.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:323)
   at android.arch.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:137)
   at android.arch.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:123)
   at android.support.v4.app.Fragment.performResume(Fragment.java:2396)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1467)
   at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1752)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1821)
   at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3251)
   at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3215)
   at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:217)
   at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:509)
   at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:498)
   at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:171)
   at android.app.Activity.performResume(Activity.java:7377)
   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3763)
   at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3828)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1746)
   at android.os.Handler.dispatchMessage(Handler.java:105)
   at android.os.Looper.loop(Looper.java:164)
   at android.app.ActivityThread.main(ActivityThread.java:6938)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

编辑:以下是Activity的resume方法:

@Override
    public void onResume() 
        super.onResume();
        updateParams();
        if (!EventBus.getDefault().isRegistered(this)) 
            EventBus.getDefault().register(this);
        
        if (addButtonClicked) 
            addButtonClicked = false;
            refreshData();
        
        if (isRestarted) 
            isRestarted = false;
            if (adapter != null && adapter.getFragmentAtPosition(0) != null && (adapter.getFragmentAtPosition(0).isAdded() || !adapter.getFragmentAtPosition(0).isDetached())) 
                ((ListFragment) adapter.getFragmentAtPosition(0)).searchList("");
            
        
        checkAndUpdateOnTabs();
    

【问题讨论】:

您能发布此活动的onResume() 方法吗?我不确定这是否是正常的 NPE。 这是否只发生在特定的操作系统版本上? 我已经用 onResume() 编辑了问题。它并没有影响特定的操作系统。 哪一行应用崩溃了? 哪个版本?我找到了this bug request on GitHub 和this question。 【参考方案1】:

生命周期扩展中的 API 已被弃用。相反,为您需要的特定生命周期工件添加依赖项。

 lifecycle_version = "2.2.0"
    def arch_version = "2.1.0"

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
    // Lifecycles only (without ViewModel or LiveData)
    implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

    // Saved state module for ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

    // Annotation processor
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
    // alternately - if using Java8, use the following instead of lifecycle-compiler
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

    // optional - helpers for implementing LifecycleOwner in a Service
    implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"

    // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

    // optional - ReactiveStreams support for LiveData
    implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"

【讨论】:

以上是关于尝试在空对象引用上调用接口方法“java.lang.Object java.util.Map$Entry.getValue()”的主要内容,如果未能解决你的问题,请参考以下文章

错误:java.lang.NullPointerException:尝试在空对象引用上调用接口方法“int java.util.List.size()”

尝试在空对象引用上调用接口方法“java.lang.Object java.util.Map$Entry.getValue()”

NPE 同时膨胀布局(尝试在空对象引用上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)')

什么错误尝试在空对象引用上调用虚拟方法'java.lang.String android.content.Context.getPackageName()'[重复]

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ActionBar.setNavigationMode(int)”

NullPointerException:尝试在空对象引用上调用虚拟方法 AlertDialog.setTitle(java.lang.CharSequence) [重复]