Java 没有错误,程序运行,但绝对正确

Java 没有错误,程序运行,但绝对正确,java,android,Java,Android,这是一个非常直接的小程序。现在保持简单。概念很简单,有一系列单选按钮,用于模型建筑中流行的不同比例。勾选相应的单选按钮,输入以英寸为单位的实际尺寸,然后点击计算,它将以英寸为单位显示刻度尺寸。简单的划分,但如果我能让它发挥作用,它将为我提供所需的信息。到目前为止,它编译干净,运行良好,你检查单选按钮,输入一个数字,然后点击计算按钮,它只是坐在那里,什么也不做。以下是我添加的代码,当然不包括进口 public void onButtonClick(View v) { int n1, re

这是一个非常直接的小程序。现在保持简单。概念很简单,有一系列单选按钮,用于模型建筑中流行的不同比例。勾选相应的单选按钮,输入以英寸为单位的实际尺寸,然后点击计算,它将以英寸为单位显示刻度尺寸。简单的划分,但如果我能让它发挥作用,它将为我提供所需的信息。到目前为止,它编译干净,运行良好,你检查单选按钮,输入一个数字,然后点击计算按钮,它只是坐在那里,什么也不做。以下是我添加的代码,当然不包括进口

 public void onButtonClick(View v)
{
    int n1, result = 0;
    EditText e1 = (EditText)findViewById(R.id.editTextSize);
    TextView t1 = (TextView)findViewById(R.id.textView3);
    RadioButton rb144 = (RadioButton)findViewById(R.id.radioButton144);
    RadioButton rb72 = (RadioButton)findViewById(R.id.radioButton72);
    RadioButton rb48 = (RadioButton)findViewById(R.id.radioButton48);
    RadioButton rb35 = (RadioButton)findViewById(R.id.radioButton35);
    RadioButton rb32 = (RadioButton)findViewById(R.id.radioButton32);
    RadioButton rb25 = (RadioButton)findViewById(R.id.radioButton25);
    RadioButton rb24 = (RadioButton)findViewById(R.id.radioButton24);
    n1 = Integer.parseInt(e1.getText().toString());
    if(rb144.isChecked()){
        result = n1 / 144;
    }else if(rb72.isChecked()){
        result = n1 / 72;
    }else if(rb48.isChecked()){
        result = n1 / 48;
    }else if(rb35.isChecked()){
        result = n1 / 35;
    }else if(rb32.isChecked()){
        result = n1 / 32;
    }else if(rb25.isChecked()){
        result = n1 / 25;
    }else if(rb24.isChecked()){
        result = n1 / 24;
    }
    t1.setText(Integer.toString(result));

OnClickListener的方法名称不正确。应该是onClick…,而不是onButtonClick。。。。见示例:

Button button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        //hello button

        TextView tv = (TextView)findViewById(R.id.textView3);
        tv.setText("hello button!");

    }
  });
将来,在定义这些方法时,一定要添加@Override注释,这样,如果拼写错误,编译器将检测到您实际上没有重写某些内容

使用以下代码:

我对你的代码做了一些修改

 public void onButtonClick(View v)
{
        float n1,result = 0;
        EditText e1 = (EditText)findViewById(R.id.editTextSize);
        TextView t1 = (TextView)findViewById(R.id.textView3);

        RadioButton rb144 = (RadioButton)findViewById(R.id.radioButton144);
        RadioButton rb72 = (RadioButton)findViewById(R.id.radioButton72);
        RadioButton rb48 = (RadioButton)findViewById(R.id.radioButton48);
        RadioButton rb35 = (RadioButton)findViewById(R.id.radioButton35);
        RadioButton rb32 = (RadioButton)findViewById(R.id.radioButton32);
        RadioButton rb25 = (RadioButton)findViewById(R.id.radioButton25);
        RadioButton rb24 = (RadioButton)findViewById(R.id.radioButton24);

        n1 =Float.parseFloat(e1.getText().toString());

        if(rb144.isChecked()){
            result = n1 / 144;
        }else if(rb72.isChecked()){
            result = n1 / 72;
        }else if(rb48.isChecked()){
            result = n1 / 48;
        }else if(rb35.isChecked()){
            result = n1 / 35;
        }else if(rb32.isChecked()){
            result = n1 / 32;
        }else if(rb25.isChecked()){
            result = n1 / 25;
        }else if(rb24.isChecked()){
            result = n1 / 24;
        }
        t1.setText(String.valueOf(result));
}

注意:为了更好的编程方法,您需要在onCreate中定义查找视图ID的内容,如单选按钮、编辑文本等,而不是单击按钮

旁注:整数除法。您输入的实际值是多少?设置了哪个单选按钮?发布调用OnButton的代码单击方法logCat中有错误吗?它基本上是根据选中的单选按钮将框中的内容除以一个数字。没有错误,但我认为没有鲁尔兹可能把这里最大的问题弄对了。虽然有一个点击监听器和所有的东西,我已经用代码做了所有的建议,但没有运气。我意识到,当点击特定的按钮时,似乎没有一件事能告诉它该做什么。