【问题标题】:Bluetooth onClickView error蓝牙 onClickView 错误
【发布时间】:2011-08-05 17:13:17
【问题描述】:

这是我的主要活动中的一段代码,出现错误,我不明白有什么问题?

错误是:void 是变量 onClick 的无效类型。

我是 android 和 java 的新手,所以它可能有点简单.. 谢谢。

private void startApp(){

        View Patient_Button = findViewById(R.id.patientButton);
        Patient_Button.setOnClickListener(this);
        View Doctor_Button = findViewById(R.id.doctorButton);
        Doctor_Button.setOnClickListener(this);
        View About_Option = findViewById(R.id.aboutButton);
        About_Option.setOnClickListener(this);
        View Exit_Option = findViewById(R.id.exit);
        Exit_Option.setOnClickListener(this);

Error:        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.patientButton:
                Intent b = new Intent(this, Detailed_ModeActivity.class);
                startActivity(b);
                break;
            case R.id.doctorButton:
                Intent a = new Intent(this, Detailed_ModeActivity.class);
                startActivity(a);
                break;
            case R.id.aboutButton:
                Intent i = new Intent(this, About.class);
                startActivity(i);
                break;
            case R.id.exit:
                finish();
                break;
            }

        };

    }

我知道该错误与 void 有关,但它通常在之前有效

【问题讨论】:

    标签: java android methods bluetooth onclick


    【解决方案1】:

    将 onClick 移出 startApp :)

    private void startApp(){
    
        View Patient_Button = findViewById(R.id.patientButton);
        Patient_Button.setOnClickListener(this);
        View Doctor_Button = findViewById(R.id.doctorButton);
        Doctor_Button.setOnClickListener(this);
        View About_Option = findViewById(R.id.aboutButton);
        About_Option.setOnClickListener(this);
        View Exit_Option = findViewById(R.id.exit);
        Exit_Option.setOnClickListener(this);
    }
    
     public void onClick(View v) {
            switch (v.getId()) {
            case R.id.patientButton:
                Intent b = new Intent(this, Detailed_ModeActivity.class);
                startActivity(b);
                break;
            case R.id.doctorButton:
                Intent a = new Intent(this, Detailed_ModeActivity.class);
                startActivity(a);
                break;
            case R.id.aboutButton:
                Intent i = new Intent(this, About.class);
                startActivity(i);
                break;
            case R.id.exit:
                finish();
                break;
            }
    
     }
    

    【讨论】:

    • 谢谢,但我还有一个问题?该项目将编译,但当我尝试在手机上运行时它强制关闭:它说应用程序已意外停止
    • 你有logcat(登录eclipse视图或者sdk/tools/ddms),有错误
    猜你喜欢
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 2011-07-13
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    相关资源
    最近更新 更多