表达式显示 id 而不是值
我有以下代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
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 usingfindviewbyid()
.