【问题标题】:Null pointer exception when calling measure on inflated layout在膨胀布局上调用度量时出现空指针异常
【发布时间】:2011-08-21 20:39:33
【问题描述】:

所以我有一个单独文件的布局,所以我对其进行了膨胀。然后我更改布局中的文本视图和图像,并调用 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);

这里是 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 给出的值是合法的。

有什么想法吗?

谢谢!

【问题讨论】:

  • 把 xml 文件给我们,漂亮的 :)
  • 请发布您的logcat和xml,也许这些可以进一步解释错误。
  • 您确定inflatedLayout 不为空吗?
  • 什么时候放大视图?如果在 onCreate 中,您还无法获得测量值,因为视图尚未调整大小
  • 我在一个方法中手动扩展视图,而不是在 onCreate 中,因为这个视图不是(也不能)是正在使用的当前视图的一部分。

标签: java android android-layout


【解决方案1】:

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

【讨论】:

  • thaaanks 很多.. 花了我很多时间来弄清楚:)
  • 此解决方案不适用于所有设备。放大视图时,我最终通过了父母。
  • @AZ_ 是的,这可能是另一种选择,但在充气时传递父母会更容易!
【解决方案2】:

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

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

【讨论】:

  • 我在 Galaxy S3 上遇到了同样的问题,但在 Nexus 5,4 和 Galaxy S4 上没有。这个解决方案对我有用。谢谢
  • 三星手机总是出现这样的错误,实际上这个解决方案适用于每一个在这种情况下给出空指针异常的设备
  • 对我来说只发生在 HTC Sensation 上,我认为这是由于 API 级别(冰淇淋),即:比 Jelly Bean 低的任何东西
  • 这个解决方案在 Android 4.3 的 Genymotion 模拟器上为我工作
  • 它会影响所有版本的 Android 4.x,但 Eric 的解决方案解决了这个问题。 Android 5 及更高版本无需额外代码即可很好地处理它。
【解决方案3】:

Inflater 不提取 layout_widthlayout_height 属性,当您不将其父分配为 inflate 方法中的参数时,请尝试将 LayoutParams 设置为指定的宽度和高度,然后调用 measure .

【讨论】:

    【解决方案4】:

    我不知道您为什么要手动充气,以及为什么要保存它的图像。但是我在主屏幕小部件的复杂布局中做几乎相同的事情,这就是我所做的:

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

    【讨论】:

    【解决方案5】:

    虽然我发现设置布局参数(如其他几个答案)确实解决了我的问题,但我使用的解决方案最终是视图被夸大了

    inflater.inflate(layoutId, null);
    

    而不是

    inflater.inflate(layoutId, parent, false);
    

    这对我的用例来说更正确,因为实际上有一个父级,并且可以使用和尊重父级的布局参数。

    【讨论】:

      【解决方案6】:

      我通过添加来修复它:

      view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,desiredHeight));
      

      在从 InfoWindowAdapter.getInfoWindow 返回之前到视图

      (来自:https://stackoverflow.com/a/15750783/4291264

      【讨论】:

        【解决方案7】:

        刚刚发现测试设备或模拟器 android 版本 代码:

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

        【讨论】:

          【解决方案8】:

          改变

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

          如果使用android 4.3以下的api调用view.measure()方法时崩溃

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多