我的一些代码被跳过为什么?

时间:2013-06-06 07:37:16

标签: java android

我一直在尝试编写这个程序,我想我几乎已经拥有它了,但我错误地认为我的代码中的一部分因为什么原因而被忽略了?当我去运行我的程序并按下行按钮时,文本显示显示输入Y而不是X,所以我知道注释掉的行之间的代码没有运行,或者是和不正常工作。所以我的问题是为什么代码的一部分被滑过而没有运行?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cad);
    ourSurface = new GLSurfaceView(this);
    FrameLayout v = (FrameLayout) findViewById(R.id.display);
    v.addView(ourSurface);
    ourSurface.setRenderer(new GLRenderer());

    final TextView info = (TextView)findViewById(R.id.info);
    Button line = (Button) findViewById(R.id.line);
    final Button enter = (Button)findViewById(R.id.enter);
    EditText cl = (EditText)findViewById(R.id.cl);
    final String value = cl.getText().toString();

    line.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                coords = Float.parseFloat(value);
            } catch (NumberFormatException e){};

//------------------------------------------------

            info.setText("Input x");
            enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    linep.add((float)coords);                   
                }
            });             
//-----------------------------------------         
            info.setText("Input y");
            enter.setOnClickListener(new View.OnClickListener() {               
                @Override
                public void onClick(View v) {
                    linep.add((float)coords);
                    indexP.add((short)p);
                }
            });
        }
    }): 
}

1 个答案:

答案 0 :(得分:1)

它没有跳过任何东西 - 它正在做它应该做的事情......似乎你不明白听众是如何工作的:

you set text of info to "input x" 
then you set the on click listener of enter
then you set text of info to "input y"
then you set the on click listener of enter again

所以最后 两条第一条线由第二条线复位...... 就像这样

infotext = "input x"
enterclick = some code
infotext = "input y"
enderclick = some other code

Line buton甚至没有设置点击监听器,所以它什么都不做。