Reputation: 71
I am trying to make a simple calculator in android.
Details:
I have multiple buttons, and have set the onClick method in all of the button views. What i do is get the ID of individual by the switch statement. For testing purpose i have just made the percent button clickable, and tried to show the text of the button in the EditText.
But When I click the button the app crashe.
this is my code:
public class MainActivity extends Activity {
Button show_cam_ctrl,
show_voice_ctrl,
cal_btn_clear,
cal_btn_del,
cal_btn_percent,
cal_btn_divide,
cal_btn_plus,
cal_btn_pi,
cal_btn_root,
cal_btn_dot,
cal_btn_equal,
cal_btn_power,
cal_btn_ptrl,
cal_btn_sin, cal_btn_cos, cal_btn_tan,
cal_btn_sinIn, cal_btn_cosIn, cal_btn_tanIn,
cal_btn_0, cal_btn_1, cal_btn_2, cal_btn_3, cal_btn_4, cal_btn_5, cal_btn_6, cal_btn_7, cal_btn_8, cal_btn_9;
TextView ma_res_txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Control Buttons
show_cam_ctrl = (Button) findViewById(R.id.show_cam_ctrl);
show_voice_ctrl = (Button) findViewById(R.id.show_voice_ctrl);
show_cam_ctrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,cam_calculator.class);
startActivity(i);
}
});
show_voice_ctrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,voice_calculator.class);
startActivity(i);
}
});
}
//Calculator main page logic
public void calculate(View v){
ma_res_txt = (TextView) findViewById(R.id.ma_res_txt);
String t;
int length = ma_res_txt.getText().length();
switch (v.getId()){
case R.id.cal_btn_clear:{
ma_res_txt.setText("");
break;
}
case R.id.cal_btn_del:{
String text = ma_res_txt.getText().toString();
ma_res_txt.setText(text.substring(0, text.length() - 1));
}
case R.id.cal_btn_percent:{
t = "%";
ma_res_txt.setText(t);
break;
}
}
}
@Override
public void finish() {
super.finish();
}
}
logcat:
07-29 12:19:10.734 27279-27279/com.example.nadeemahmad.smartcalculator E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.nadeemahmad.smartcalculator, PID: 27279 java.lang.IllegalStateException: Could not find method calculate()(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'cal_btn_percent' at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:5076) at android.view.View$DeclaredOnClickListener.onClick(View.java:5035) at android.view.View.performClick(View.java:6205) at android.widget.TextView.performClick(TextView.java:11103) at android.view.View$PerformClick.run(View.java:23653) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Upvotes: 0
Views: 1506
Reputation: 3422
Define id of all Buttons like
show_cam_ctrl = (Button) findViewById(R.id.show_cam_ctrl);
and then define particular function in particular button click event like below code;
show_cam_ctrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate();
}
});
and then code for particular function outside of onCreate() method.
Upvotes: 1
Reputation: 611
check your xml button code if you added calculate() in onClick then remove ()
<Button
android:id="@+id/cal_btn_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="calculate()" />
to change remove ()
<Button android:id="@+id/cal_btn_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="calculate" />
Upvotes: 1