Monday, 12 November 2018

"Attempt to read from field 'int android.content.pm.ApplicationInfo.targetSdkVersion' on a null object reference" exception

While running Espresso tests using Mockito, I am currently having this issue that "Attempt to read from the field 'int android.content.pm.ApplicationInfo.targetSdkVersion' on a null object reference" exception is randomly appearing after the main Activity was initiated.

This is the init part for the main Activity:

private MainActivity subject;
SingleActivityFactory<MainActivity> activityFactory =
    new SingleActivityFactory<MainActivity>(MainActivity.class) {
        @Override
        protected MainActivity create(Intent intent) {
            subject = spy(getActivityClassToIntercept());
            return subject;
        }
    };

@Rule public ActivityTestRule<MainActivity> mainActivityRule =
    new ActivityTestRule<>(activityFactory, true, false);

@Before
public void init() {
    mainActivityRule.launchActivity(null);
}

This is the exception:

java.lang.NullPointerException: Attempt to read from field 'int 
android.content.pm.ApplicationInfo.targetSdkVersion' on a null object 
reference
at android.view.View.getLayoutDirection(View.java:10277)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:446)
at android.view.View.measure(View.java:23169)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1059)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535
Test running failed: Instrumentation run failed due to 'Process crashed.'

Also, the logcat of crash:

2018-11-06 15:34:50.391 3299-3299/com.application.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.application.android, PID: 3299
java.lang.NullPointerException: Attempt to read from field 'int android.content.pm.ApplicationInfo.targetSdkVersion' on a null object reference
    at android.view.View.getLayoutDirection(View.java:10277)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:446)
    at android.view.View.measure(View.java:23169)
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1059)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)

The issue here is that this problem is not consistent - failure ration is about 40%. Does anybody have any insights about this issue? Also, I should mention that this issue is not appearing if the activity is launched without spying Activity using Mockito.



from "Attempt to read from field 'int android.content.pm.ApplicationInfo.targetSdkVersion' on a null object reference" exception

No comments:

Post a Comment