Java 在膨胀布局上调用度量值时出现空指针异常
所以我有一个单独的文件布局,所以我膨胀它。然后,我更改布局中的文本视图和图像,并调用measure,但当我尝试测量布局时,会得到一个空指针。我一辈子都搞不懂为什么会这样。我试图最终将布局转换为位图Java 在膨胀布局上调用度量值时出现空指针异常,java,android,android-layout,Java,Android,Android Layout,所以我有一个单独的文件布局,所以我膨胀它。然后,我更改布局中的文本视图和图像,并调用measure,但当我尝试测量布局时,会得到一个空指针。我一辈子都搞不懂为什么会这样。我试图最终将布局转换为位图 View inflatedView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.my_layout, null); inflatedView.measure(getWidth(), getHeight()); in
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);
这是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>
在measure()行,它抛出了一个空指针,我已经验证了getWidth和getHeight是否给出了合法的值
有什么想法吗
谢谢 我不知道你为什么要手动充气,为什么要保存它的图像。但我在主屏幕小部件的复杂布局中做了几乎相同的事情,我就是这么做的:
LayoutInflater inflater = LayoutInflater.from(context);
View content = inflater.inflate(layoutId, null);
int measuredWidth = View.MeasureSpec.makeMeasureSpec(widgetWidth, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(widgetHeight, View.MeasureSpec.EXACTLY);
content.measure(measuredWidth, measuredHeight);
content.layout(0, 0, content.getMeasuredWidth(), content.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(widgetWidth, widgetHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
File file = new File(bitmapCacheFileName);
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
充气器
在充气
方法中未将其父参数指定为参数时,不会提取布局宽度
和布局高度
属性,因此请尝试使用指定的宽度和高度设置布局参数
,然后调用度量。度量()
引发空指针异常
,因为当您充气布局时,它不会读取布局高度宽度属性
只需在调用view.measure()
之前添加这一行,具体取决于您使用的布局类型
v、 度量(度量等级makemeaspect(0,度量等级未指定),度量等级makemeaspect(0,度量等级未指定))
最高级别的相对论者似乎在无边界的测量中发疯。在调用measure之前,在膨胀视图上设置显式边界:
v.setLayoutParams(new ViewGroup.LayoutParams(0,0));
虽然我发现设置布局参数(就像在其他几个答案中一样)确实解决了我的问题,但我使用的解决方案最终导致视图膨胀
inflater.inflate(layoutId, null);
而不是
inflater.inflate(layoutId, parent, false);
这对于我的用例更为正确,因为实际上有一个父级,并且可以使用和尊重父级的布局参数。我通过添加以下内容来修复它:
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,desiredHeight));
在从InfoWindowAdapter.getInfoWindow返回视图之前,将其复制到视图
(from:)刚刚发现测试设备或仿真器android版本<4.4这会生成空指针异常。用线性布局改变布局可以解决这个问题。这里我改变了你的代码。
代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
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>
改变
View inflatedView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.my_layout, null);
到
如果使用android 4.3下面的api调用view.measure()方法时崩溃,请提供xml,请:)请发布您的日志和xml,也许这些可以进一步解释错误。您是否确保
inflatedLayout
不为空?您何时扩展视图?如果在onCreate中,由于视图尚未调整大小,您还无法获取测量值。我正在使用方法手动扩展视图,而不是在onCreate中,因为此视图不是(也不能)正在使用的当前视图的一部分。使用我的代码?你从哪里得到NullPointerException?在哪一行?看完整的代码,这不会引发很多NullPointerException thaaanks。。我花了很多时间才弄明白:)这个解决方案并不适用于所有设备。在膨胀视图时,我最终通过了一位家长。@穆罕默德·巴巴尔请看这个@AZ_uu是的,这可能是另一个选项,但在膨胀时通过一位家长会更容易!我在Galaxy S3上得到了同样的问题,但在Nexus 5、4和Galaxy S4上没有得到同样的问题。这个解决方案对我有效。ThanksSamsung手机总是会出现这样的错误,实际上这个解决方案会在每一个出现空指针异常的设备上运行,对我来说,只有在HTC Sensation上发生过这种情况,我认为这是由于API级别(冰激凌)ie:Jelly Bean之外的任何东西该解决方案在Android 4.3的Genymotion emulator上对我有效,它影响所有版本的Android 4.x,但Eric的解决方案解决了这个问题。Android 5及以上版本在没有额外代码的情况下可以很好地处理它。
View inflatedView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.my_layout, null);
View inflatedView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.my_layout, null,false);
inflatedView.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));