2011-05-09 47 views
21

所以我有一个布局是一个单独的文件,所以我充气。然后,我改变了文本的意见和形象在我的布局,并调用的措施,但我得到一个空指针,当我尝试测量布局。我不能为了我的生活找出原因。我试图最终将布局转换为位图。空指针异常上充气布局调用度量时

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> 

在度量()行,它抛出一个空指针,而我已验证的getWidth和getHeight是给合法的值。

任何想法?

谢谢!

+0

给我们的xml,请相信:) – Dan 2011-05-09 17:19:08

+0

请发布您的logcat和xml,或许这些可以进一步解释错误。 – JoxTraex 2011-05-09 17:19:08

+0

你确定'inflatedLayout'不是null吗? – MByD 2011-05-09 17:23:09

回答

37

measure()抛出Null Pointer Exception,因为当你吹你的布局也不会读布局高度width属性。

调用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; 
    } 
+2

thaaanks很多..花了我很多时间来弄清楚:) – cV2 2012-11-12 15:16:14

+0

此解决方案不适用于所有设备。当我膨胀视图时,我最终传递了父项。 – 2014-11-15 12:36:01

+0

@ MuhammadBabar看到这个请http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html – 2014-11-17 01:31:37

1

我不知道为什么你的手膨胀,为什么要保存它的图像。但我在一个复杂的布局的主屏幕小部件做几乎同样的事情,这是我做的:

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(); 
+0

我仍然得到一个空指针异常:\ – rozap 2011-05-09 17:39:18

+0

与我的代码?你在哪里得到NullPointerException?在哪一行? – Kaj 2011-05-09 17:45:17

+0

看到这个完整的代码,这不会抛出NullPointerException http://stackoverflow.com/a/11047006/185022 – 2012-07-11 06:39:24

11

Inflater不提取layout_widthlayout_height属性,当你不其父指​​定为在inflate方法的参数,所以要尽量设定LayoutParams具有特定的宽度和高度,然后调用量度。

33

顶级的RelativeLayout似乎吓坏了的措施,如果没有边界。调用措施之前设置的充气观点明确的界限:

v.setLayoutParams(new ViewGroup.LayoutParams(0,0)); 
+0

感谢这工作对我 – 2014-03-07 20:16:53

+2

我在Galaxy S3上得到同样的问题,但不是在Nexus 5,4和Galaxy S4上。解决方案为我工作...谢谢 – 2014-05-21 05:48:23

+0

这也是工作... – 2014-12-23 10:45:28

1

虽然我发现,设置布局参数(如在其他几个答案)没有解决我的问题,我用结束了该观点与膨胀的解决方案

inflater.inflate(layoutId, null); 

代替

inflater.inflate(layoutId, parent, false); 

这是对我的使用情况更正确的,因为实际上有一个家长和家长的布局参数可以使用和尊重。

1

刚刚发现,测试设备或仿真器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> 

0

变化

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)); 

如果崩溃,invoki ng view.measure()使用android 4.3以下的api方法