Android Studio无法解决错误setOnClickListener

时间:2016-08-24 14:27:04

标签: java android-studio

嗨,我似乎无法建立这个......它说它无法解决OnClickListener。 onClick操作执行返回主活动的后退按钮。

Button bnCompute = (Button) this.findViewById(R.id.bnCompute);
    bnCompute.setOnClickListener(new View.OnClickListener());

    {
        @Override

        public void onClick (View view){
        Toast.makeText(MainActivity.this, "You Compute All!", Toast.LENGTH_LONG).show();


        EditText etBeauty = (EditText) MainActivity.this.findViewById(R.id.etBeauty);
        EditText etBody = (EditText) MainActivity.this.findViewById(R.id.etBody);
        EditText etIntelligence = (EditText) MainActivity.this.findViewById(R.id.etIntelligence);

        int total = Integer.parseInt(String.valueOf(etBeauty.getText())) + Integer.parseInt(String.valueOf(etBody.getText()))
                + Integer.parseInt(String.valueOf(etIntelligence.getText()));

        Intent actSummary = new Intent(MainActivity.this, Score.class);
        actSummary.putExtra("total", Integer.toString(total));
        MainActivity.this.startActivity(actSummary);
    }



}

1 个答案:

答案 0 :(得分:1)

您已在侦听器范围之外实现了onClick。 它应该是这样的:

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

    //Your mistake is on this line.
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
        }

    });