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
162 views
in Technique[技术] by (71.8m points)

android - 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.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

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

1 Reply

0 votes
by (71.8m points)

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


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

...