在膨胀布局上调用度量时出现空指针异常

roz*_*zap 21 java android android-layout

所以我有一个单独的文件布局,所以我给它充气.然后我在我的布局中更改我的文本视图和图像,并调用measure,但是当我尝试测量布局时,我得到一个空指针.我无法为我的生活找出原因.我试图最终将布局转换为位图.

View inflatedView  = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.my_layout, null);

inflatedView.measure(getWidth(), getHeight());
inflatedView.layout(0, 0, inflatedView.getMeasuredWidth(),inflatedView.getMeasuredHeight());
Bitmap viewAsImage = Bitmap.createBitmap(inflatedView.getWidth(), inflatedView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas viewCanvas = new Canvas(viewAsImage);
Run Code Online (Sandbox Code Playgroud)

这是XML

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_gravity="bottom"
    android:id="@+id/my_layout"
    android:background="@color/transparent_black">
    <ImageView android:id="@+id/image_left"
        android:layout_width="48dip" android:layout_height="48dip"
        android:layout_centerVertical="true" android:layout_alignParentLeft="true"
        android:scaleType="fitXY" android:maxWidth="48dip" android:maxHeight="48dip"
        android:padding="5dip" />
    <ImageView android:id="@+id/image_right"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginRight="20dip" android:src="@drawable/arrow"
        android:layout_centerVertical="true" android:scaleType="fitXY"
        android:maxWidth="48dip" android:maxHeight="48dip"
        android:layout_alignParentRight="true" />
    <TextView android:paddingLeft="4dip" android:paddingRight="1dip"
        android:layout_width="wrap_content"         android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image_left"
        android:layout_toLeftOf="@id/image_right"       android:id="@+id/some_name"
        android:maxLines="1" android:ellipsize="end" android:textSize="20sp"
        android:textColor="@color/white"
                    android:layout_centerVertical="true"
        android:textStyle="normal" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在measure()行,它抛出一个空指针,我已经验证getWidth和getHeight给出了合法的值.

有任何想法吗?

谢谢!

AZ_*_*AZ_ 39

measure()抛出Null Pointer Exception因为当你给布局膨胀时它不会读取布局高度宽度属性.

只需在调用之前添加此行,view.measure()具体取决于您使用的布局类型

v.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));

public Bitmap getBitmapFromView(RelativeLayout v) {
        v.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
        Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }
Run Code Online (Sandbox Code Playgroud)

  • thaaanks很多..花了我很多时间来弄清楚:) (2认同)

小智 34

顶级的RelativeLayout似乎在没有边界的情况下吓坏了.在调用measure之前在膨胀视图上设置显式边界:

v.setLayoutParams(new ViewGroup.LayoutParams(0,0));
Run Code Online (Sandbox Code Playgroud)

  • 我在Galaxy S3上遇到了同样的问题,但在Nexus 5,4和Galaxy S4上却没有.这个解决方案对我有用.谢谢 (2认同)

Key*_*ari 11

Inflater如果不在方法中将其父项指定为参数,则不会提取layout_widthlayout_height属性inflate,因此请尝试LayoutParams使用指定的宽度和高度进行设置,然后调用measure.