Android兼容性问题 -- RelativeLayout.mesure()出现NullPointerException异常

View的高度获取问题

在Android中一个View被创建后是不能够立刻知道其宽度和高度的。
如下代码试图在footerView创建后,通过其getWidth()和getHeight()方法得到其宽度和高度,实际得到的width和height都等于0。

View footerView = View.inflate(this, R.layout.list_footer, null);
int width = footerView.getWidth();
int height = footerView.getHeight();

这是因为,在Android中一个View在创建完成后还需要经过测量(measure),布局(layout)和绘制(draw)三个步骤才会显示出来。其宽度和高度通常只有在布局完成后才能够获取到,如果在布局之前就尝试获取其宽度和高度,只能得到0。
因此,如果要获取一个View的宽度和高度,通常可以等待其布局完成后再尝试获取。在Android中提供了一些监听事件,可以在布局完成,或绘制之前通知监听者。
如下代码均可以得到footerView的宽度和高度。

footerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        footerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        int wight = footerView.getWidth();
        int height =footerView.getHeight();
    }
});
footerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        footerView.getViewTreeObserver().removeOnPreDrawListener(this);
        int wight = footerView.getWidth();
        int height =footerView.getHeight();
        return false;
    }
});

View高度的手动测量

虽然可以通过注册监听的方式来获取View的宽度和高度,但这时通常都已经完成了布局,如果我们需要在获取到View的宽度和高度后再对View的布局做额外的调整,要么是没有效果,要么是需要重新布局,影响效率,如果界面已经绘制完成再去调整布局,可能还会导致界面出现一些闪烁。因此,对这类需要根据某个View的宽度或高度做重新布局的时候,就需要在View创建完成后能够立刻得到其宽度和高度。要想实现这点,可以通过手动执行View的measure方法。示例代码如下。

int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
footerView.measure(width, height);
int width = footerView.getMeasuredWidth();
int height =footerView.getMeasuredHeight();

RelativeLayout 执行measure方法异常

上述代码对根Layout为FrameLayout,LinearLayout的View都能够正常工作,但是对根Layout为RelativeLayout的View,可能会出现如下的NullPointerException异常。
这里写图片描述

问题原因

产生此异常的原因是RelativeLayout在Android4.4以下版本中的一个bug。此bug已在Android4.4版本中修正。以下是Android4.0和Android4.4版本RelativeLayout的部分源码对比。
这里写图片描述
可以看到在Android4.0版本中没有判断mLayoutParams是否为null,如果mLayoutParams为null就会抛出NullPointerException异常。而在Android4.4版本中增加了对mLayoutParams是否为null的判断。

解决方法

由于此bug只在RelativeLayout中存在,只需要在RelativeLayout布局外面再嵌套一层FrameLayout或者LinearLayout即可。

已标记关键词 清除标记
我在Activity中创建了一个新的Thread。通过输入流,run()方法获取vector对象。现在我想在名称为direc的ListView中显示vector对象的内容。但是程序没有正常运行,抛出了异常。 相关的代码如下: public class FileExplorerActivity extends Activity implements Runnable { public static final String TAG="ricky"; Vector<String> dirs = new Vector<String>(); ListView direc; static ArrayAdapter<String> arrayAdapter; Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fileexplorer); direc = (ListView) findViewById(R.id.directoriesss); handler = new Handler(); Thread thread = new Thread(this); thread.start(); } public FileExplorerActivity() { } public void run() { Log.d(TAG, "FileExplorer: Thread started"); try { Log.d(TAG, "FileExplorer: reading data"); dirs = (Vector<String>) ConnectionThread.din.readObject(); Log.d(TAG, "FileExplorer: read data"); arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, android.R.id.text2 , dirs); Log.d(TAG, "FileExplorer: ArrayAdapter created"); handler.post( new Runnable() { public void run() { Log.d(TAG, "FileExplorer: In handler Thread"); direc.setAdapter(arrayAdapter); Log.d(TAG, "FileExplorer: Updated UI"); } }); Log.d(TAG, "FileExplorer: Directory set"); }catch (Exception e) { Log.d(TAG, "FileExplorerActivity: Exception:"+e.getMessage()); } } } Logcat的错误: > 1. 12-20 20:14:46.601 7759 7834 D ricky : FileExplorer: Thread started > 2. 12-20 20:14:46.601 7759 7834 D ricky : FileExplorer: reading data > 3. 12-20 20:14:46.648 7759 7834 D ricky : FileExplorer: read data > 4. 12-20 20:14:46.648 7759 7834 D ricky : FileExplorer: ArrayAdapter created > 5. 12-20 20:14:46.648 7759 7834 D ricky : FileExplorer: Directory set > 6. 12-20 20:14:46.656 7759 7759 D ricky : FileExplorer: In handler Thread > 7. 12-20 20:14:46.656 7759 7759 D ricky : FileExplorer: Updated UI > 8. 12-20 20:14:46.664 7759 7759 D AndroidRuntime: Shutting down VM > 9. 12-20 20:14:46.664 7759 7759 W dalvikvm: threadid=1: thread exiting with uncaught 10. exception (group=0x4001e560) > 11. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: FATAL EXCEPTION: main > 12. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: java.lang.NullPointerException > 13. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353) > 14. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323) > 15. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:1456) > 16. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.ListView.measureHeightOfChildren(ListView.java :1291) > 17. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.ListView.onMeasure(ListView.java:1202) > 18. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.view.View.measure(View.java:8355) > 19. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java :3279) > 20. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017) > 21. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.LinearLayout.measureHorizontal(LinearLayout.ja va:701) > 22. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:311) > 23. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.view.View.measure(View.java:8355) > 24. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java :3279) > 25. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) > 26. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.view.View.measure(View.java:8355) > 27. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java :531) > 28. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) > 29. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.view.View.measure(View.java:8355) > 30. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java :3279) > 31. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) > 32. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.view.View.measure(View.java:8355) > 33. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.view.ViewRoot.performTraversals(ViewRoot.java:882) > 34. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.view.ViewRoot.handleMessage(ViewRoot.java:1917) > 35. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) > 36. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.os.Looper.loop(Looper.java:130) > 37. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:3859) > 38. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) > 39. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:507) > 40. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) > 41. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) > 42. 12-20 20:14:46.687 7759 7759 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
©️2020 CSDN 皮肤主题: 像素格子 设计师:CSDN官方博客 返回首页