Java Android - 类型 View 中的 setOnClickListener(View.OnClickListener) 不适用于参数 (new OnClickListener(){})

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16708684/
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-10-31 23:44:10  来源:igfitidea点击:

Java Android - setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})

javaandroidonclicklistener

提问by Thomas VC

When using the following code i keep getting errors, i know there is another way of using the onclick functions, android:onclick... but i prefer this 'cleaner' way.

使用以下代码时,我不断收到错误消息,我知道还有另一种使用 onclick 函数的方法,android:onclick...但我更喜欢这种“更干净”的方式。

the id name of the textview is right but i keep getting this error with the onclicklistener.

textview 的 id 名称是正确的,但我一直在使用 onclicklistener 收到此错误。

what am i doing wrong?

我究竟做错了什么?

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

    TextView text = (TextView) findViewById(usernameText);
    text.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            // Do some job here

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_screen, menu);
    return true;
}`

回答by Manuel Pires

The problem may be a jar that you imported that has an onClickListener...

问题可能是您导入的 jar 具有 onClickListener ...

Try add this:

尝试添加这个:

text.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        // Do some job here

    }
});

回答by Yevgeny Simkin

without seeing your error, I'd guess that you probably imported the wrong OnClickListener. Make sure that it's the View one.

没有看到您的错误,我猜您可能导入了错误的 OnClickListener。确保它是视图之一。

回答by Luc

May be you should take your screenshot and upload here, but please looking for your code:

可能您应该截取屏幕截图并在此处上传,但请查找您的代码:

Ensure you

确保您

import View.OnClickListener

回答by Taj Mb

TextView text = (TextView) findViewById(usernameText);
text.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        // Do some job here

    }
});