开发者

Expressions is showing id instead of value

开发者 https://www.devze.com 2023-03-18 20:13 出处:网络
I\'ve got the following code: private EditText LeftBeam, BeamDistance, RightBeam; private TextView Distance;

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).

开发者_Python百科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);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消