Skip to content Skip to sidebar Skip to footer

Not Sure Why Getting Duplicate Method OnClick(View)

I've got one activity here is what I am trying do. When app starts an image is displayed. When the image is clicked the next image is displayed. I've got it all setup with main.xm

Solution 1:

when you use

image1.setOnClickListener(this);
image2.setOnClickListener(this);

you have to override the default onClick() Method in th Android API:

@Override
public void onClick(View v)

also setting in xml android:onClick="onClick"

will let you handle clik event from your custom onClick Method

and that's way an exception is throwed to you: telling that you have a duplicate method!!

so you have two choices:

  1. remove android:onClick="onClick": and that's what i would do

  2. remove image1.setOnClickListener(this); image2.setOnClickListener(this); and handle clicks on your way on the custom onClick() method


Solution 2:

Now trying to setup onclick for the image so the next image will be displayed. I've added android:onClick="onClick" to the image vew in main.xml.

If you have added android:onClick="onClick" to the both image views in main.xml You should remove image1.setOnClickListener(this); and image2.setOnClickListener(this); from code since you have already specified it in main xml.


Solution 3:

Here is the code...

public class MainActivity extends Activity {

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


    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            final Dialog dl = new Dialog(MainActivity.this);

            dl.setContentView(R.layout.cdialog);

            dl.setTitle("Title");


            TextView txt = (TextView) dl.findViewById(R.id.textView1);

            txt.setText("Hi \n How are you \n" +
                    "Where were you \n Good to see you");

            Button btn = (Button) dl.findViewById(R.id.button1);
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    dl.dismiss();

                }
            });
            dl.show();
        }
    });
}
}

Post a Comment for "Not Sure Why Getting Duplicate Method OnClick(View)"