Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.xml and imageview. The app builds and runs and displays the first image no problem. 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. I am using (this) for setOnClickListner and I implemented View.onClickListener for the class but the switch case I setup I get duplicate method for onClick(View v) and I'm not sure why.

Also trying to figure out the findViewById I think the way I have it image1 will always be displayed.

getting error on line public void onClick(View v) that it is a duplicate method.

Here is the code I have. Thanks for any assistance on this.

main.xml is a linear layout not sure that matters.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView
    android:id="@+id/imageView1"
    android:src="@drawable/bear"        
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:onClick="onClick"
    android:contentDescription="@string/desc"/>

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/bear_n"
    android:onClick="onClick"
    android:contentDescription="@string/desc" />

This is the .java file.

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;


public class VTFCActivity extends Activity implements View.OnClickListener{

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

    image = (ImageView) findViewById(R.id.imageView1);
    ImageView image1 = (ImageView) findViewById(R.id.imageView1);
    ImageView image2 = (ImageView) findViewById(R.id.imageView2);

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

}
public void onClick(View v) {
    switch (v.getId()){

    case R.id.imageView1:
        image.setImageResource(R.drawable.bear);
        break;
    case R.id.imageView2:
        image.setImageResource(R.drawable.bear_n);
        break;          
    }
}
See Question&Answers more detail:os

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...