2016-08-24 76 views
0

嗨,我似乎無法建立此..它聲明,它不能解決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); 
    } 



} 

回答

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

    }); 
+0

非常感謝! :) –