android开发中遇到的问题解决之RelativeLayout执行measure(w,h)方法时,出现NullPointerException的异常情况

转载地址:http://blog.csdn.net/ljz2009y/article/details/18351491

RelativeLayout执行measure(w,h)方法时,出现NullPointerException的异常情况


在编写下啦刷新的项目代码的时候,在Listview的HeaderView中的head.xml文件中,根布局为RelativeLayout的时候,在计算headerView.measure的时候,出现空指针异常,当将更布局改为Linearlayout就运行正常了。


在思考为何在RelativeLayout出现异常的问题的时候,在查阅官方网站的时候,我注意到这个段话:

Note: In platform version 17 and lower, RelativeLayout was affected by a measurement bug that could cause child views to be measured with incorrect MeasureSpec values. (See MeasureSpec.makeMeasureSpec for more details.) This was triggered when a RelativeLayout container was placed in a scrolling container, such as a ScrollView or HorizontalScrollView. If a custom view not equipped to properly measure with the MeasureSpec mode UNSPECIFIED was placed in a RelativeLayout, this would silently work anyway as RelativeLayout would pass a very large AT_MOST MeasureSpec instead.

This behavior has been preserved for apps that set android:targetSdkVersion="17" or older in their manifest's uses-sdktag for compatibility. Apps targeting SDK version 18 or newer will receive the correct behavior

翻下来就是说:

在android系统版本在17级以下(包含17的时候)。使用measure会出现NULL异常情况,这个是一个BUG。原因是在RelativeLayout的控件使用在含有scrolling的时候,该含有scrolling的控件中计算空间大小的时候,没有使用MeasureSpec mode UNSPECIFIED的布局方式在RelativeLayout。自定义的控件则会尽可能的使用AT_MOST 来替换对齐方式。

如果你想解决这个问题有2个方法:

1.讲SDK的目标版本升级

2.将需要使用RelativeLayout的上层包一个LinearLayout即可、


关于具体的核心解释,我也不太理解,希望大牛阅读到的时候能够回复一下,谢谢


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以在点击第一个按钮,动态地向 RelativeLayout 添加另一个按钮。具体步骤如下: 1. 在布局文件定义一个 RelativeLayout,并在其添加一个 Button: ``` <RelativeLayout android:id="@+id/relative_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me"/> </RelativeLayout> ``` 2. 在代码获取 RelativeLayout 和 Button 对象,并为 Button 设置点击事件: ``` RelativeLayout relativeLayout = findViewById(R.id.relative_layout); Button button1 = findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 在 RelativeLayout 添加另一个 Button Button button2 = new Button(MainActivity.this); button2.setText("New Button"); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ); params.addRule(RelativeLayout.BELOW, button1.getId()); relativeLayout.addView(button2, params); } }); ``` 3. 在点击事件动态创建一个新的 Button,并设置它的文本和布局参数,然后添加到 RelativeLayout 。这里的布局参数使用 RelativeLayout.LayoutParams,并设置了它的位置规则,使得新的 Button 位于第一个 Button 的下方。 注意:如果你需要在点击第二个 Button 出现第三个 Button,可以在第二个 Button 的点击事件重复上述步骤即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值