Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

android - setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (SequencerActivity) back.setOnClickListener(this);

I want to display an image on button click, but I have three errors in my code. What's wrong?

  1. class name "SequencerActivity"
    

    The type SequencerActivity must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int).

  2. next.setOnClickListener(this);
    

    The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (SequencerActivity).

  3. onClick(View v)
    

The method onClick(View) of type SequencerActivity must override or implement a supertype method.

Here's the code giving those errors:

public class SequencerActivity extends Activity implements OnClickListener
{
private int imageCounter = 0;
private ImageView imageLoader;

private int[] imageList = {R.drawable.f03, R.drawable.f04, R.drawable.f05, R.drawable.f06};

@Override
public void onCreate(Bundle savedInstanceState) 
{
    setContentView(R.layout.main);//this one is the common parent layout for all image views
    super.onCreate(savedInstanceState);

    /*requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);*/

    //int image1 = R.drawable.image_w_lbl_0;

    imageLoader = (ImageView) findViewById(R.id.imageLoader);
    //imageLoader.setImageResource(image1);

    Button next = (Button) findViewById(R.id.next);
    Button back = (Button) findViewById(R.id.back);
    next.setOnClickListener(this);
    back.setOnClickListener(this);
    back.setEnabled(false);
    //show the default image
    this.loadImage(imageList[imageCounter]);

}
@Override
public void onClick(View v) 
{
    int imagePath = 0;
    // TODO Auto-generated method stub
    switch (v.getId())
    {
    case R.id.next:
        Log.i("Tag","tag");
        if(imageCounter < imageList.length)
        {
            imageCounter++;
            imagePath = imageList[imageCounter];
            if (imageCounter==(imageList.length)-1)
            {
                { 
                    ImageButton next=(ImageButton)findViewById(R.id.next); 
                    next.setEnabled(false); 
                }
            }
            else
            {
                ImageButton back=(ImageButton)findViewById(R.id.back); 
                back.setEnabled(true); 
            }
        }
        break;
    case R.id.back:
        if(imageCounter > 0)
        {
            imageCounter--;
            imagePath = imageList[imageCounter];
            if (imageCounter==0) 
            { 
                ImageButton back=(ImageButton)findViewById(R.id.back); 
                back.setEnabled(false); 
            }
            else
            {
                ImageButton next=(ImageButton)findViewById(R.id.next); 
                next.setEnabled(true);
            }
        }
        break;
    }
    this.loadImage(imagePath);

}

private void loadImage(int imagePath)
{
    imageLoader.setImageResource(imagePath);

}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The OnClickListener that you implement is not correct, try to implement View.OnClickListener and not DialogInterface.OnClickListener.

You can see that in your import

import View.OnClickListener

instead of

import DialogInterface.OnClickListener


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...