jordan jordan - 1 month ago 25
Java Question

Android Studio method Never Used

I would Like to know why in Android Studio my calculator app crashes because it could not find my method.

Here is my MainActivity.java

public void OnClick(View v) {
Button button = (Button) v;
String str = button.getText().toString();

EditText edit = (EditText) findViewById(R.id.editText);
edit.setText(total);
}


Here is the preview of my calc, all number buttons are set onClick to go to the OnClick method in my MainActivity. (as you can see to the right of the picture)

activity_main.xml

Can anyone tell me what i am doing wrong here?
Thank you

Answer
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);


    Button button = (Button) findViewById(R.id.yourButtonId);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            EditText edit = (EditText) findViewById(R.id.editText);
            edit.setText(total);
        }
    });

}
Comments