Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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.

Update:

The issue was fixed by introducing changes into View.java class:

@ResolvedLayoutDir
public int getLayoutDirection() {
    //added check if ApplicationInfo is not null
    if(getContext().getApplicationInfo() == null){
            View v = this;
            Log.i("View id: ", getContext().getString(v.getId()));
            Log.e("Main error", "Bug catched");
    }
    else {
        final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
        if (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
            return LAYOUT_DIRECTION_RESOLVED_DEFAULT;
        }
    }
    return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
            PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
}

Would it be possible to achieve similar result without modifying the system class?

like image 510
Tomas March Avatar asked Nov 06 '18 14:11

Tomas March


1 Answers

I think your issue connected to previous issue,

Which produce similar behavior when using getFragmentManager() method,

Solution is:

use getSupportFragmentManager() instead and extend from v4.app.Fragment class

like image 50
user7294900 Avatar answered Sep 30 '22 12:09

user7294900