表达式显示 id 而不是值

发布于 2024-11-18 22:22:23 字数 1394 浏览 5 评论 0原文

我有以下代码:

private EditText LeftBeam, BeamDistance, RightBeam;
private TextView Distance;
private float HeigtDifferenceBeams;
private float HeightLeftBeam, HeightRightBeam;


HeightLeftBeam = Float.parseFloat(LeftBeam.getText().toString());
HeightRightBeam = Float.parseFloat(RightBeam.getText().toString());

HeigtDifferenceBeams = Math.abs(HeightLeftBeam - HeightRightBeam);
DistanceBetweenBeams = Math.sqrt((Math.pow(HeigtDifferenceBeams, 2) + Math.pow(Float.parseFloat(BeamDistance.getText().toString()), 2)));

Distance.setText(String.format("%.2f",DistanceBetweenBeams));

当我在我的设备上运行此代码时,我得到一个 FC。 (07-08 20:22:43.399:错误/AndroidRuntime(25183):java.lang.RuntimeException:无法启动活动ComponentInfo {com.tricky_design.x / com.tricky_design.x.main}:java.lang.NullPointerException

当我在调试模式下运行它,它进入表达式视图“DistanceBetweenBeams”vis 值(id=830126972712)。

我做错了什么?

非常感谢您的帮助。

编辑

我已经实例化了使用的 editText 和 TextViews:

    LeftBeam = (EditText) findViewById (R.id.LeftBeam);
    LeftBeam.setOnLongClickListener(LongClickListener);
    LeftBeam.addTextChangedListener(TextChanged);

    RightBeam = (EditText) findViewById (R.id.RightBeam);
    RightBeam.setOnLongClickListener(LongClickListener);
    RightBeam.addTextChangedListener(TextChanged);

    Distance = (TextView) findViewById (R.id.DistanceTextView);

I've got the following code:

private EditText LeftBeam, BeamDistance, RightBeam;
private TextView Distance;
private float HeigtDifferenceBeams;
private float HeightLeftBeam, HeightRightBeam;


HeightLeftBeam = Float.parseFloat(LeftBeam.getText().toString());
HeightRightBeam = Float.parseFloat(RightBeam.getText().toString());

HeigtDifferenceBeams = Math.abs(HeightLeftBeam - HeightRightBeam);
DistanceBetweenBeams = Math.sqrt((Math.pow(HeigtDifferenceBeams, 2) + Math.pow(Float.parseFloat(BeamDistance.getText().toString()), 2)));

Distance.setText(String.format("%.2f",DistanceBetweenBeams));

When I run this on my device I get a FC. (07-08 20:22:43.399: ERROR/AndroidRuntime(25183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tricky_design.x/com.tricky_design.x.main}: java.lang.NullPointerException

When I run it in debug mode it get in the expression view "DistanceBetweenBeams"vis value (id=830126972712).

What is it that i'm doing wrong?

Already many thanks for your help.

Edit

I did instantiate the used editText and TextViews:

    LeftBeam = (EditText) findViewById (R.id.LeftBeam);
    LeftBeam.setOnLongClickListener(LongClickListener);
    LeftBeam.addTextChangedListener(TextChanged);

    RightBeam = (EditText) findViewById (R.id.RightBeam);
    RightBeam.setOnLongClickListener(LongClickListener);
    RightBeam.addTextChangedListener(TextChanged);

    Distance = (TextView) findViewById (R.id.DistanceTextView);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱给你人给你 2024-11-25 22:22:23

您的 Distance TextView 未初始化,因此它为 null。这就是问题所在。

Distance 需要对布局中的 TextView 进行对象引用,通常使用 findviewbyid()

Your Distance TextView is not initialised, so it's null. That's the problem.

Distance needs to be made an object reference to a TextView in your layout, typically using findviewbyid().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文