gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:07:54 25 4
gpt4 key购买 nike

我正在尝试使用

https://github.com/nisrulz/screenshott

用于截图的库。

但它给出了错误。

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

相对布局:

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

我该如何解决我的问题?如何获得相对布局 View ?

最佳答案

你不能在 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

关于java - 尝试在空对象引用上调用虚拟方法 'void android.view.View.measure(int, int)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63314768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com