如何解决null对象引用?

时间:2016-09-02 11:34:51

标签: android android-layout

如何设置图像progragmaticall的宽度?

这是我的代码,

    txtone = (ImageView)findViewById(R.id.imageView);
    txttwo = (ImageView)findViewById(R.id.imageView2);
    height = metrics.heightPixels;
    width = metrics.widthPixels;
    if (height == 800 && width == 1217) {
    int a = txtone.getMeasuredWidth();
    txtone.getLayoutParams().width = 280;
    txttwo.getLayoutParams().width = 280;
    txtthree.getLayoutParams().width = 280;
    txtone.requestLayout();
    txttwo.requestLayout();
    txtthree.requestLayout();

1 个答案:

答案 0 :(得分:1)

在实际初始化之前,您尝试使用txttwo txtthree之一。当你打电话

时,其中一个是null
txttwo.getLayoutParams().width = 280;
txtthree.getLayoutParams().width = 280;

至于您的代码,您没有初始化txtthree