java.lang.NoSuchMethodException:的onClick在Android列表视图 [英] java.lang.NoSuchMethodException: onClick in list view on android

查看:322
本文介绍了java.lang.NoSuchMethodException:的onClick在Android列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我用列表视图图像按钮。我写为每个图像按钮操作。但我得到java.lang.NoSuchMethodException:的onClick例外。我不知道其中的原因错误请帮助我。如何写的图像按钮的动作。提前致谢。

 公共类InventoryListActivity扩展ListActivity {
  私人InventoryAdapter适配器;
    公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.inventory_list);
    适配器=新InventoryAdapter(本);
    setListAdapter(适配器);
 }
}

在InventoryAdapter活动:

 公共类InventoryAdapter扩展BaseAdapter实现观测,OnClickListener {
公共InventoryAdapter(上下文CTX){
    上下文= CTX;
    库存= IAPManager.shared()getInventory()。
    inventory.addObserver(本);
    inventory.load();
}公共无效更新(可观察到0,对象ARG){    开关(((存量)ARG).getStatus()){
    案例加载:
        this.notifyDataSetChanged();
        打破;
    默认:
        打破;
    }
}@覆盖
公众诠释的getCount(){
    返回inventory.size(Inventory.FilterType.ALL);
}@覆盖
公共对象的getItem(INT位置){
    返回inventory.getProducts(Inventory.FilterType.ALL)获得(位置);
}@覆盖
众长getItemId(INT位置){
    返回的位置;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    产品产品=(产品)的getItem(位置);    查看视图。    如果(convertView == NULL){
        LayoutInflater吹气=(LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        鉴于= inflater.inflate(R.layout.inventory_list_item,NULL);
    }    其他{
        鉴于= convertView;
    }    玩=(的ImageButton)findViewById(R.id.imageButton2);
    细节=(的ImageButton)findViewById(R.id.imageButton3);
    买=(的ImageButton)findViewById(R.id.imageButton2);    play.setTag(位置);
    detail.setTag(位置);
    buy.setTag(位置);    titleview的=(TextView中)view.findViewById(R.id.product_title);
    titleView.setText(product.getTitle());
    ICONVIEW =(ImageView的)view.findViewById(R.id.product_icon);    串iconURL = product.getIconURLString();    如果(AsyncImageLoader.contains(iconURL)){
        iconView.setImageDrawable(AsyncImageLoader.get(iconURL));
    }    其他{
        iconView.setImageDrawable(NULL);
        新AsyncImageLoader(product.getIconURLString(),新AsyncImageLoader.Delegate(){
            公共无效imageLoaded(字符串urlString,可绘制imageDrawable){
                notifyDataSetChanged();
            }
        });
    }    descriptionView =(TextView中)view.findViewById(R.id.product_description);
    descriptionView.setText(product.getDescription());    返回视图。
}私人的ImageButton findViewById(INT imagebutton2){
    // TODO自动生成方法存根
    返回null;
}@覆盖
公共无效的onClick(视图v){    Log.e(onclick事件的onclick);
    整数位置=(整数)v.getTag();
    开关(v.getId()){
    案例R.id.imageButton1:
       Log.e(买入,买入位置+位置);    打破;
    案例R.id.imageButton2:
        Log.e(玩,播放位置+位置);
    打破;
    案例R.id.imageButton3:
        Log.e(详细信息,详细位置+位置);
    打破;
    }
}

}

在XML:

 < ImageButton的机器人:背景=@绘制/ play_btn机器人:可聚焦=false的机器人:的onClick =的onClick
            机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
            机器人:ID =@ + ID / imageButton2机器人:layout_weight =0.5/>


解决方案

我认为这个问题是,你试图通过XML要连接onClick的监听器,但在适配器中实现它。我认为Android根本就不承认onClick的监听器或不知道是应该用于ImageButton的。尽量实现和动态的getView()方法中添加OnClickListener。

更新

检查第一,如果getView()方法的声明。在那里,OnClickListener将为3个按键进行设置。当您的适配器实现OnClickListener接口传递的adpater的方法。您也可以删除安卓的onClick =的onClick从布局xml文件的ImageButtons 属性。

  @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        产品产品=(产品)的getItem(位置);        查看视图。        如果(convertView == NULL){
            LayoutInflater吹气=(LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            鉴于= inflater.inflate(R.layout.inventory_list_item,NULL);            玩=(的ImageButton)findViewById(R.id.imageButton2);
            细节=(的ImageButton)findViewById(R.id.imageButton3);
            买=(的ImageButton)findViewById(R.id.imageButton2);            //设置OnClickListener为3图像按钮。
            play.setOnClickListener(本);
            detail.setOnClickListener(本);
            buy.setOnClickListener(本);        }        其他{
            鉴于= convertView;
        }        玩=(的ImageButton)findViewById(R.id.imageButton2);
        细节=(的ImageButton)findViewById(R.id.imageButton3);
        买=(的ImageButton)findViewById(R.id.imageButton2);        play.setTag(位置);
        detail.setTag(位置);
        buy.setTag(位置);        titleview的=(TextView中)view.findViewById(R.id.product_title);
        titleView.setText(product.getTitle());
        ICONVIEW =(ImageView的)view.findViewById(R.id.product_icon);        串iconURL = product.getIconURLString();        如果(AsyncImageLoader.contains(iconURL)){
            iconView.setImageDrawable(AsyncImageLoader.get(iconURL));
        }        其他{
            iconView.setImageDrawable(NULL);
            新AsyncImageLoader(product.getIconURLString(),新AsyncImageLoader.Delegate(){
                公共无效imageLoaded(字符串urlString,可绘制imageDrawable){
                    notifyDataSetChanged();
                }
            });
        }        descriptionView =(TextView中)view.findViewById(R.id.product_description);
        descriptionView.setText(product.getDescription());        返回视图。
   }

in my app i use image button in list view. i write action for each image button. but i get java.lang.NoSuchMethodException: onClick exception. i do not know the reason for error please help me. how to write action for the image buttons . thanks in advance.

public class InventoryListActivity extends ListActivity {
  private InventoryAdapter adapter;
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inventory_list);
    adapter = new InventoryAdapter(this);
    setListAdapter(adapter);
 }
}

in InventoryAdapter activity:

public class InventoryAdapter extends BaseAdapter implements Observer,OnClickListener {


public InventoryAdapter(Context ctx) {     
    context = ctx;
    inventory = IAPManager.shared().getInventory();
    inventory.addObserver(this);
    inventory.load();
}

public void update(Observable o, Object arg) {

    switch ( ((Inventory)arg).getStatus() ) {
    case LOADED:
        this.notifyDataSetChanged();
        break;
    default:
        break;
    }
}

@Override
public int getCount() {
    return inventory.size(Inventory.FilterType.ALL);
}

@Override
public Object getItem(int position) {
    return inventory.getProducts(Inventory.FilterType.ALL).get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Product product = (Product) getItem(position);

    View view;

    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.inventory_list_item, null);
    }

    else {
        view = convertView;
    }

    play = (ImageButton)findViewById(R.id.imageButton2);
    detail = (ImageButton)findViewById(R.id.imageButton3);
    buy = (ImageButton)findViewById(R.id.imageButton2);

    play.setTag(position);
    detail.setTag(position);
    buy.setTag(position);

    titleView = (TextView) view.findViewById(R.id.product_title);
    titleView.setText(product.getTitle());


    iconView = (ImageView) view.findViewById(R.id.product_icon);

    String iconURL = product.getIconURLString();

    if(AsyncImageLoader.contains(iconURL)) {
        iconView.setImageDrawable(AsyncImageLoader.get(iconURL));
    }

    else {
        iconView.setImageDrawable(null);
        new AsyncImageLoader(product.getIconURLString(), new AsyncImageLoader.Delegate(){
            public void imageLoaded(String urlString, Drawable imageDrawable) {
                notifyDataSetChanged();
            }
        });
    }

    descriptionView = (TextView) view.findViewById(R.id.product_description);
    descriptionView.setText(product.getDescription());

    return view;
}

private ImageButton findViewById(int imagebutton2) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onClick(View v) {

    Log.e("onclick","onclick");
    Integer position = (Integer) v.getTag();
    switch(v.getId()){
    case R.id.imageButton1:
       Log.e("Buy","buy position"+position);

    break;
    case R.id.imageButton2:
        Log.e("play","play position"+position);
    break;
    case R.id.imageButton3:
        Log.e("detail","detail  position"+position);
    break;  
    }       
}

}

in xml:

            <ImageButton android:background="@drawable/play_btn"    android:focusable="false" android:onClick="onClick"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/imageButton2" android:layout_weight="0.5" />

解决方案

I think the problem is, that you're trying to wire up the onClick listener via xml but implement it in your adapter. I think android simply doesn't recognize the onClick listener or doesn't know that is should be used for the ImageButton. Try to implement and add the OnClickListener dynamically in the getView() method.

UPDATE

Check the first if statement of the getView() method. There the OnClickListener will be set for the 3 buttons. As your adapter implements the OnClickListener interface you pass the adpater to the method. You also have to remove the android:onClick="onClick" attribute from your ImageButtons in the layout xml file.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Product product = (Product) getItem(position);

        View view;

        if(convertView == null) {
            LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.inventory_list_item, null);

            play = (ImageButton)findViewById(R.id.imageButton2);
            detail = (ImageButton)findViewById(R.id.imageButton3);
            buy = (ImageButton)findViewById(R.id.imageButton2);

            // Set the OnClickListener for the 3 image buttons. 
            play.setOnClickListener(this);
            detail.setOnClickListener(this);
            buy.setOnClickListener(this);

        }

        else {
            view = convertView;
        }

        play = (ImageButton)findViewById(R.id.imageButton2);
        detail = (ImageButton)findViewById(R.id.imageButton3);
        buy = (ImageButton)findViewById(R.id.imageButton2);

        play.setTag(position);
        detail.setTag(position);
        buy.setTag(position);

        titleView = (TextView) view.findViewById(R.id.product_title);
        titleView.setText(product.getTitle());


        iconView = (ImageView) view.findViewById(R.id.product_icon);

        String iconURL = product.getIconURLString();

        if(AsyncImageLoader.contains(iconURL)) {
            iconView.setImageDrawable(AsyncImageLoader.get(iconURL));
        }

        else {
            iconView.setImageDrawable(null);
            new AsyncImageLoader(product.getIconURLString(), new AsyncImageLoader.Delegate(){
                public void imageLoaded(String urlString, Drawable imageDrawable) {
                    notifyDataSetChanged();
                }
            });
        }

        descriptionView = (TextView) view.findViewById(R.id.product_description);
        descriptionView.setText(product.getDescription());

        return view;
   }

这篇关于java.lang.NoSuchMethodException:的onClick在Android列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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