java - 我的一些代码被跳过了,为什么?

标签 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);
                }
            });
        }
    }): 
}

最佳答案

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

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

线路按钮甚至没有设置点击监听器,因此它什么也不做。

关于java - 我的一些代码被跳过了,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16956299/

相关文章:

php - undefined variable : query_params

java - 如何通过按钮更改操作栏的颜色?

java - Android 命名约定中的匈牙利表示法

java - Android Studio的Gradle Sync(SDK)问题

Java 简单日期格式化程序显示错误值

java - 使用java获取REST Web服务中http请求的内容

java - Android 在新线程中运行类中的函数

Android Emulator 显示系统 UI 在启动时没有响应?

java - 您知道的所有 Maven Archetype 目录的 URL 是什么?

java - org.springframework.web.bind.MissingServletRequestParameterException异常