【问题标题】:Attempt to invoke virtual method 'void android.view.View.measure(int, int)' on a null object reference尝试在空对象引用上调用虚拟方法 'void android.view.View.measure(int, int)'
【发布时间】:2020-11-28 13:22:05
【问题描述】:

我正在尝试使用

https://github.com/nisrulz/screenshott

要截屏的库。

但它给出了错误。

尝试调用虚方法 'void android.view.View.measure(int, int)' 在空对象引用上

相对布局:

<?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>

OnCreate方法:

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.

如何解决我的问题?如何获得相对布局视图?

【问题讨论】:

  • 尝试替换 findViewById(R.id.layoutview);用 findViewById(R.layout.layoutView);
  • 不,您的解决方案不起作用。
  • 你能发布完整的堆栈跟踪吗?没有它就很难判断出了什么问题。
  • 尝试调试看看myView是否为空。如果它为空,那么它要么不存在于您的布局中,要么库中发生了一些事情,如果没有您发布堆栈跟踪,我无法完全判断。

标签: java android android-layout nullpointerexception


【解决方案1】:

你可以试试.takeScreenShotOfJustView(view) 代替.takeScreenShotOfView(view) 所以,使用

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

这将无限制地截取屏幕截图...

【讨论】:

    【解决方案2】:

    这不会给你 NPE,而是 SS:

    科特林:

    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:我使用了与上面提到的相同的库,但没有得到 NPE

    【讨论】:

    • 您使用的takeScreenShotOfJustView 与问题中的takeScreenShotOfView 有什么区别?
    【解决方案3】:

    视图绘制后需要截图 所以当视图绘制完成时你需要一个监听器 你可以使用 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);
                }
            }
        });
    

    【讨论】:

    • symbol 'view' 是我的视图的名称,但在您的代码中输入您的视图名称,例如 'myView'
    【解决方案4】:

    您一定不要在onCreate 中使用它。在这个 onCreate 方法中,您的布局被测量和膨胀,但您的 UI 尚未创建。

    您必须在onCreate 之后调用您的代码。例如在按钮事件中:

    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

    【讨论】:

    • “布局测量”、“布局膨胀”、“UI 未创建”是什么意思? UI 是由布局定义的,它是在创建布局时创建的。观看次数被setContentView 夸大。但它不是立即测量的。顺序很重要。
    【解决方案5】:

    请这样写RelativeLayout myView = findViewById(R.id.layoutview); 希望xml布局文件的名字是activity_main.xml

    【讨论】:

    • 两种情况下的结果相同,因为两种情况下的函数调用相同:findViewById(R.id.layoutview)。转换为 RelativeLayout 然后返回到 View 是丑陋的,是的,但除此之外什么都不做。这应该是一个提高代码可读性的注释。
    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多