无法让 .getwidth() 和 .getheight() 工作

Can't get .getwidth() and .getheight() to work

我知道还有很多关于这个主题的其他问题,但我已经浏览了所有这些问题,但仍然没有解决。

我制作了这段代码用于测试:

public class MainActivity extends Activity {

RelativeLayout layout;
TextView widthtext;
TextView heighttext;
int width;
int height;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layout = (RelativeLayout) findViewById(R.id.rela);

    widthtext = (TextView) findViewById(R.id.textView);
    heighttext = (TextView) findViewById(R.id.textView2);

    layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            width = layout.getWidth();
            height = layout.getHeight();
        }
    });

    widthtext.setText(width);
    heighttext.setText(height);
}

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/rela">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="123"
    android:id="@+id/textView"
    android:layout_alignTop="@+id/textView2"
    android:layout_toLeftOf="@+id/textView2"
    android:layout_toStartOf="@+id/textView2"
    android:layout_marginRight="50dp"
    android:layout_marginEnd="50dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="123"
    android:id="@+id/textView2"
    android:layout_marginRight="50dp"
    android:layout_marginEnd="50dp"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

经过很多麻烦后,我现在注意到 width 和 height int 具有正确的值,尽管我无法让它们显示在我的 TextView 中,获取 NullPointerExeption。

谁能完成这项工作?

额外:

public class MainActivity extends Activity {

RelativeLayout layout;
TextView widthtext;
TextView heighttext;
int width;
int height;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layout = (RelativeLayout) findViewById(R.id.rela);

    widthtext = (TextView) findViewById(R.id.textView);
    heighttext = (TextView) findViewById(R.id.textView2);

    layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            width = layout.getWidth();
            height = layout.getHeight();
        }
    });

    widthtext.setText(width + "");
    heighttext.setText(height + "");
}

}

你正在做这个

widthtext.setText(width);
heighttext.setText(height);

在您的听众之外,因此它自然还没有值。

将它们移到里面

layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        width = layout.getWidth();
        height = layout.getHeight();

        // move here
        widthtext.setText(width);
        heighttext.setText(height);
    }
});
}

作为 blackbelt pointed out in a , you are calling setText() 并传递 int。这将查找该值的资源 ID。将它们更改为 Strings

widthtext.setText("" + width);
heighttext.setText("" + height);

setText 取字符串值

所以而不是

setText(width);
setText(height);

setText(width+"");
setText(height+"");