【发布时间】: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