Thomas VC
Thomas VC

Reputation: 67

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

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.

the id name of the textview is right but i keep getting this error with the 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;
}`

Upvotes: 3

Views: 14561

Answers (4)

Taj Mb
Taj Mb

Reputation: 61

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

    }
});

Upvotes: 0

Luc
Luc

Reputation: 2805

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

Ensure you

import View.OnClickListener

Upvotes: 0

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28439

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

Upvotes: 3

Manuel Pires
Manuel Pires

Reputation: 637

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

Try add this:

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

    }
});

Upvotes: 7

Related Questions