Ben
Ben

Reputation: 185

OnClickListener cannot be resolved to a type (Eclipse)

Hello im new to programming, im trying to construct my first simple application, im looking to play a short soundclip on the push of an ImageButton.

while typing out my code i get an error with the statement;

 Button.setOnClickListener(new OnClickListener() {

The on click listener is underlined and when i go to the error eclipse tells me that OnClickListener cannot be resolved to a type.

Here is my code:

import android.app.Activity;
import android.os.Bundle;
import android.view.view;
import android.view.view.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

final ImageButton Button = (ImageButton) findViewById(R.id.imageButton1);
Button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // Perform action on clicks

    }
});

I read a suggestion that said to add;

import android.view.view;

aswell as

import android.view.view.OnClickListener;

These import statements are also highlighted. Could these errors be caused by how eclipse is set up on my computer?

Any help would be greatly appreciated

Upvotes: 7

Views: 34088

Answers (6)

vthg2themax
vthg2themax

Reputation: 1

If you are using the new Android Studio you must declare your new OnClickListener as View.OnClickListener. Otherwise Android Studio gets confused and won't understand.

Upvotes: 0

Mohamud
Mohamud

Reputation: 132

if you still have error you can make the class abstract like this public abstract class MainActivity extends Activity implements OnClickListener {

Upvotes: 0

zamil nizar
zamil nizar

Reputation: 177

Add

import android.view.View.OnclickListener

to your import section and it should work.

Upvotes: 4

Justin
Justin

Reputation: 800

make sure your class implements OnClickListener

public class main extends Activity implements OnClickListener {

Upvotes: 0

antonyt
antonyt

Reputation: 21883

The second "view" in the import statement is a class (hence, OnClickListener is an inner class/interface) and should be capitalised:

import android.view.View.OnClickListener;

Upvotes: 0

Kasra Rahjerdi
Kasra Rahjerdi

Reputation: 2503

For starters, it's always best to let Eclipse manage all imports by tapping Ctrl+Shift+O when you see an import error.

It seems that your problem is due to:

import android.view.view;

Which should be:

import android.view.View;

Same goes with android.view.View.OnClickListener.

If you remove the two lines you've manually added and hit Ctrl+Shift+O, everything should fix itself.

Upvotes: 12

Related Questions