Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Attempt to invoke virtual method ‘void android.view.View.measure(int, int)’ on a null object reference

Posted on November 14, 2022

Solution 1 :

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

Solution 2 :

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);
            }
        }
    });

Solution 3 :

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

Solution 4 :

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…

Solution 5 :

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

Problem :

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?

READ  [ANSWERED] python - kivy.network.urlrequest not working on Android, causes black screen
Powered by Inline Related Posts

Comments

Comment posted by Shreemaan Abhishek

try replacing findViewById(R.id.layoutview); with findViewById(R.layout.layoutView);

Comment posted by gurkan stack

No, your solution is not working.

Comment posted by Eugen Pechanec

Can you post the full stacktrace? It’s hard to tell what’s wrong without it.

Comment posted by Furkan Yurdakul

Try debugging and see if

Comment posted by Eugen Pechanec

What do you mean by “layout measured”, “layout inflated”, “UI not created”? UI is defined by layout, it’s created when layout is created. Views are inflated by

Comment posted by AmrDeveloper

symbol ‘view’ is the name of my view but in your code put your view name for example ‘myView’

Comment posted by Eugen Pechanec

You’re using

Comment posted by Eugen Pechanec

The result is the same in both cases because the function call is the same in both cases:

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress