Android Studio不能解决错误setOnClickListener

问题描述:

嗨,我似乎无法建立此..它声明,它不能解决OnClickListener。 onClick操作执行后退按钮进入主要活动。Android Studio不能解决错误setOnClickListener

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



} 

您已经实现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(); 
     } 

    }); 
+0

非常感谢! :) –