不知道为什么越来越重复的方法的onClick(视图) [英] Not sure why getting Duplicate method onClick(View)

查看:149
本文介绍了不知道为什么越来越重复的方法的onClick(视图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动
这里是我想要做的。当应用程序启动时显示的图像。当点击图像显示下一个图像。
我有它的main.xml和ImageView的所有设置。
该应用程序生成并运行,并显示第一个图像没有问题。
现在想安装的onclick为图像,从而使下一个图像就会显示出来。我已经添加了安卓的onClick =的onClick在main.xml中的形象VEW。
我使用(本)为setOnClickListner和我实现View.onClickListener的类,但开关情况下,我的设置我得到的onClick(视图V)重复的方法,我不知道为什么。

也试图找出findViewById我想我有这IMAGE1将始终显示的方式。

在线公共无效的onClick(视图V),这是一个重复的方法,得到错误

下面是code我有。感谢有这方面的援助。

main.xml中是一个线性布局不是问题肯定。

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>< ImageView的
    机器人:ID =@ + ID / imageView1
    机器人:SRC =@绘制/熊
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    安卓的onClick =的onClick
    机器人:contentDescription =@字符串/递减/>< ImageView的
    机器人:ID =@ + ID / imageView2
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:SRC =@绘制/ bear_n
    安卓的onClick =的onClick
    机器人:contentDescription =@字符串/递减/>

这是java文件。

 进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ImageView;
公共类VTFCActivity扩展活动实现View.OnClickListener {ImageView的形象;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    图像=(ImageView的)findViewById(R.id.imageView1);
    ImageView的此搜索=(ImageView的)findViewById(R.id.imageView1);
    ImageView的IMAGE2 =(ImageView的)findViewById(R.id.imageView2);    image1.setOnClickListener(本);
    image2.setOnClickListener(本);}
公共无效的onClick(视图v){
    开关(v.getId()){    案例R.id.imageView1:
        image.setImageResource(R.drawable.bear);
        打破;
    案例R.id.imageView2:
        image.setImageResource(R.drawable.bear_n);
        打破;
    }
}


解决方案

当您使用

  image1.setOnClickListener(本);
image2.setOnClickListener(本);

您必须覆盖默认的onClick()方法中日Android的API:

  @覆盖
公共无效的onClick(视图v)

也设置在XML 安卓的onClick =的onClick

将让您从您的自定义的onClick方法处理CLIK事件

这就是一个方式异常扔你:告诉你有一个重复的方法!

所以你有两个选择:


  1. 删除安卓的onClick =的onClick:这就是我会做


  2. 删除 image1.setOnClickListener(本); image2.setOnClickListener(本); 和处理点击在您的自定义方式的onClick()方法


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;          
    }
}

解决方案

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

这篇关于不知道为什么越来越重复的方法的onClick(视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆