没有错误和程序运行,但绝对是JACK

时间:2014-03-05 05:46:11

标签: java android

这是一个非常简单的小程序。现在保持简单。概念很简单,有一堆单选按钮,适用于不同尺度的模型建筑。勾选相应的单选按钮,以英寸为单位输入实际大小,然后点击计算,它会以英寸为单位吐出比例尺寸。简单的划分,但如果我可以让它工作,它将获得我需要的信息。到目前为止,它编译干净,运行正常,你检查单选按钮,输入一个数字,然后点击计算按钮,它只是坐在那里,什么都不做。以下是我添加的代码(当然不包括导入)。

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

2 个答案:

答案 0 :(得分:0)

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注释,这样如果拼错了编译器会检测到你实际上没有覆盖某些东西的东西。

答案 1 :(得分:0)

使用此代码:

我对您的代码进行了一些更改。

 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()而不是Button Click上定义查找视图ID的内容,例如单选按钮,edittext等。