我有以下代码:
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:ERROR / AndroidRuntime(25183):java.lang.RuntimeException:无法启动活动ComponentInfo {com.tricky_design.x / com.tricky_design.x.main}:java.lang.NullPointerException < / p>
当我在调试模式下运行时,它会进入表达式视图“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);
答案 0 :(得分:1)
您的Distance
TextView未初始化,因此它为空。那就是问题所在。
Distance
需要在布局中成为TextView的对象引用,通常使用findviewbyid()
。