android - java.lang.NoSuchMethodException : onClick in list view on android 异常

标签 android android-layout android-listview

在我的应用程序中,我在 ListView 中使用图像按钮。我为每个图像按钮编写 Action 。但我得到 java.lang.NoSuchMethodException: onClick 异常。我不知道错误的原因请帮助我。如何为图像按钮编写 Action 。提前致谢。

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

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

在 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" />

最佳答案

对于我们这些固执且不想制作 OnClickListener 的人,这里有一个例子:

http://developer.android.com/reference/android/widget/Button.html

注意:要将 XMLs android:onClick 字段挂接到您的 Activity 方法,您需要具有正确的签名,即:

void YourMethod(View v);

关于android - java.lang.NoSuchMethodException : onClick in list view on android 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6387911/

相关文章:

java - 如何在 Android 的 TableLayout 中完全隐藏一行?

android - Activity 中的动画背景

android - 隐藏View的平移动画

java - 如何为我的 BaseAdapter ListView 实现 Collection 按钮?

android - 可以在 android studio 中自动填充代码语句

android - 有没有一种方法可以在 Android 浏览器上使用来模糊网页的内容?

android - 我在 EditText 上复制并粘贴图像,但 EditText 显示 'obj'

java - 代号一日历 UpdateButtonDayDate() 问题

android - "...must supply a layout_height..."但它在我的风格资源中?

android - Android 中的 ListView 适配器问题