2011-05-09 44 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方法