Attempt to invoke virtual method 'void android.view.View.measure(int, int)' on a null object reference

Viewed 581

I am trying to use

https://github.com/nisrulz/screenshott

library to take screenshot.

But it gives error.

Attempt to invoke virtual method 'void android.view.View.measure(int, int)' on a null object reference

Relativelayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context="x.y.z"
    android:orientation="vertical"
    android:id="@+id/layoutview">

    <!--
    <com.google.android.gms.ads.AdView
        android:id="@+id/adViewChat2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="x">
    </com.google.android.gms.ads.AdView>


-->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="English"
        android:background="@drawable/shape"
        android:textAppearance="?android:attr/textAppearanceMedium"

        android:layout_marginLeft="@dimen/_15sdp"
        android:layout_marginRight="@dimen/_15sdp"
        android:textAlignment="center"

        android:id="@+id/textviewtop"
        android:layout_marginTop="@dimen/_5sdp"
        android:textColor="#ffffff"
        android:textStyle="bold"


        />




    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        android:orientation="vertical"
        android:layout_above="@+id/linear"
        android:layout_marginBottom="@dimen/_5sdp"
        android:layout_below="@+id/textviewtop"
        android:layout_marginTop="@dimen/_5sdp"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/_15sdp"
            android:background="@drawable/shape"

            android:layout_marginLeft="@dimen/_15sdp"
            android:layout_marginRight="@dimen/_15sdp"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/welcome"
                android:id="@+id/textView"
                android:textColor="#ffffff"/>

        </RelativeLayout>

    </ScrollView>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="@dimen/_15sdp"
        android:layout_marginRight="@dimen/_15sdp"
        android:background="@drawable/shape"
        android:id="@+id/linear">



        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:textColor="#ffffff"
            android:layout_weight="0.9"
            android:hint="Write a message"
            android:textColorHint="#A4A4A4"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="send"
            android:id="@+id/btn_send"
            android:layout_weight="0.1"
            android:textColor="#ffffff"

            />

    </LinearLayout>



</RelativeLayout>

in OnCreate Method:

setContentView(R.layout.activity_main);
View myView =(RelativeLayout) findViewById(R.id.layoutview);
Bitmap bitmap_view = 
    ScreenShott.getInstance().takeScreenShotOfView(myView); // problematic line, this line gives the error.

How can I solve my problem? How can I get relative layout view?

5 Answers

You must don't it in onCreate. In this onCreate method, your layout is measured and inflated but your UI is not yet created.

You must call your code later than onCreate. for example in a button event:

Button capture_screenshot = findViewById(R.id.button_takeshoot);
        capture_screenshot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View myView =(RelativeLayout) findViewById(R.id.layoutview);
                Bitmap bitmap_view = ScreenShott.getInstance().takeScreenShotOfView(myView);
            }
        });

check Activity Lifecycle

You need to take a screenshot after view drawing so you need a listener when view drawing is done you can use ViewTreeObserver.OnGlobalLayoutListener

    ViewTreeObserver observer  = view.getViewTreeObserver();
    observer .addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Bitmap bitmap_view = ScreenShott.getInstance().takeScreenShotOfView(view);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                view.getViewTreeObserver() .removeGlobalOnLayoutListener(this);
            }
        }
    });

This wont give you NPE but SS:

Kotlin:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: this method invoke")
        setContentView(R.layout.activity_main_ss)

        val rootView = window.decorView.rootView
        Log.d(TAG, "onCreate: rootView$rootView")

        val bitmap = ScreenShott.getInstance().takeScreenShotOfJustView(rootView)
    }

Java:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_ss);

        View rootView = findViewById(android.R.id.content).getRootView();
        Bitmap bitmap = ScreenShott.getInstance().takeScreenShotOfJustView(rootView);
    }

P.S: I have used the same lib as mentioned above and not getting NPE

You can try .takeScreenShotOfJustView(view) instead of .takeScreenShotOfView(view) So, use

Bitmap bitmap_view = 
    ScreenShott.getInstance().takeScreenShotOfJustView(myView);

This will take the screenshot without constraints...

please write like this RelativeLayout myView = findViewById(R.id.layoutview); hoping that the name of the xml layout file is activity_main.xml

Related