java 在ClickListener上找不到符号类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31532175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:45:58  来源:igfitidea点击:

Cannot find symbol class onClickListener

javaandroid

提问by Veeresh

I'm new to android development. Here's the problem i ran into. I'm using Android Studio. I looked up on many sites, they said to import the related class. Having done that, the problem remains. Any help is appreciated.

我是安卓开发的新手。这是我遇到的问题。我正在使用 Android Studio。我查了很多网站,他们说要导入相关的类。这样做之后,问题仍然存在。任何帮助表示赞赏。


Can anyone please help me with this, i have been searching for a while now for the solution.


任何人都可以帮我解决这个问题,我一直在寻找解决方案。

Here's the code:

这是代码:

package com.example.veeresh.myapplication;
//import statements
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        button1.setOnClickListener(
                //error: cannot find symbol class onClickListener
                new Button.onClickListener()
                {
                    public void onClick(View v)
                    {
                        TextView text1 = (TextView)findViewById(R.id.text1);
                        text1.setText("Veeresh Here");
                    }
                }
                );
    }
}

Error:

错误:

Error:(24, 27) error: cannot find symbol class onClickListener
Error:Execution failed for task ':app:compileDebugJava'.
Compilation failed; see the compiler error output for details.

错误:(24, 27) 错误:找不到符号类 onClickListener
错误:任务 ':app:compileDebugJava' 的执行失败。
编译失败;有关详细信息,请参阅编译器错误输出。

回答by sonic

It should be new View.OnClickListener()instead of new Button.onClickListener()

它应该new View.OnClickListener()代替new Button.onClickListener()

OnClickListenerwith a capital O.

OnClickListener大写O。

回答by Sachin

Edit your code like this

像这样编辑你的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button1 = (Button)findViewById(R.id.button1);
TextView text1 = (TextView)findViewById(R.id.text1);

button1.setOnClickListener(new View.onClickListener()
        {
            public void onClick(View v)
            {

                text1.setText("Veeresh Here");
            }
        }
);

回答by saikrupa

update your code with the below code

使用以下代码更新您的代码

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements android.view.View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        button1.setOnClickListener(this);
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        TextView text1 = (TextView)findViewById(R.id.text1);
        text1.setText("Veeresh Here");

    }
}

回答by Erfan GLMPR

Both Button.OnClickListener()and View.OnClickListener()works fine it is just the capital Othat you are missing.

双方 Button.OnClickListener()View.OnClickListener()工作正常,这只是资本Ø您缺少。