Android onClick方法错误

时间:2017-07-29 07:24:17

标签: android

我想在android中制作一个简单的计算器。

详细信息:

我有多个按钮,并在所有按钮视图中设置了onClick方法。我所做的是通过switch语句获取个人ID。出于测试目的,我刚刚点击了百分比按钮,并试图在EditText中显示按钮的文本。

但是当我点击按钮时,应用程序会崩溃。

这是我的代码:

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:   致命异议:主要                                                                                                    处理:com.example.nadeemahmad.smartcalculator,PID:27279                                                                                                    java.lang.IllegalStateException:找不到方法   用于android:onClick的父或祖先上下文中的calculate()(View)   在视图类android.widget.Button上使用id定义的属性   'cal_btn_percent'                                                                                                        在   android.view.View $ DeclaredOnClickListener.resolveMethod(View.java:5076)                                                                                                        在android.view.View $ DeclaredOnClickListener.onClick(View.java:5035)                                                                                                        在android.view.View.performClick(View.java:6205)                                                                                                        在android.widget.TextView.performClick(TextView.java:11103)                                                                                                        在android.view.View $ PerformClick.run(View.java:23653)                                                                                                        在android.os.Handler.handleCallback(Handler.java:751)                                                                                                        在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                                        在android.os.Looper.loop(Looper.java:154)                                                                                                        在android.app.ActivityThread.main(ActivityThread.java:6682)                                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                                        在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)                                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

2 个答案:

答案 0 :(得分:1)

如果在onClick中添加了calculate(),则检查xml按钮代码,然后删除()

  <Button 
   android:id="@+id/cal_btn_percent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="calculate()" />

更改remove()

<Button android:id="@+id/cal_btn_percent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="calculate" />

答案 1 :(得分:1)

定义所有按钮的ID,如

show_cam_ctrl = (Button) findViewById(R.id.show_cam_ctrl);

然后定义特定功能,特别是按钮点击事件,如下面的代码;

 show_cam_ctrl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         calculate();
        }
    });

然后编写onCreate()方法之外的特定函数。