Android 如何解决空对象引用?

Android 如何解决空对象引用?,android,android-layout,Android,Android Layout,如何设置图像的宽度 这是我的密码 txtone = (ImageView)findViewById(R.id.imageView); txttwo = (ImageView)findViewById(R.id.imageView2); height = metrics.heightPixels; width = metrics.widthPixels; if (height == 800 && width == 1217) { in

如何设置图像的宽度

这是我的密码

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

在实际初始化它们之前,尝试使用
txtwo
txtthree
中的一个。当您调用时,其中一个为
null

txttwo.getLayoutParams().width = 280;
txtthree.getLayoutParams().width = 280;

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

,这意味着您的txtone尚未充气,您正在ittxttwo上执行布局参数操作initialize@Aman你能告诉我怎么做吗that@Aman格罗夫卡。”框架布局。LayoutParams params=(FrameLayout.LayoutParams)txtone.getLayoutParams();'我试过了,但还是犯了同样的错误。但还是一样error@Aruna你做了什么?我没有提出解决方案,我解释了为什么会出现错误。请检查我编辑的代码。非常感谢。是的,我没有初始化txtone@Aruna,您没有初始化TXT3