java.lang.NoSuchMethodException:onClick在android上的列表视图中

时间:2011-06-17 15:13:30

标签: android android-layout android-listview

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

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活动中

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

5 个答案:

答案 0 :(得分:3)

对于我们这些顽固且不想制作OnClickListener的人来说,这是一个例子:

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

注意:要将XML:android:onClick字段挂钩到您的activity的方法,您需要拥有正确的签名,即:

void YourMethod(View v);

答案 1 :(得分:2)

我认为问题是,您正尝试通过xml连接onClick侦听器,但在适配器中实现它。我认为android根本不识别onClick监听器或者不知道应该用于ImageButton。尝试在getView()方法中动态实现和添加OnClickListener。

<强>更新

检查getView()方法的第一个if语句。在那里,将为3个按钮设置OnClickListener。当您的适配器实现OnClickListener接口时,您将adpater传递给该方法。您还必须从布局xml文件中的ImageButtons中删除android:onClick="onClick"属性。

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

答案 2 :(得分:1)

您需要从XML中删除android:onClick =“onClick”,因为ImageButton不知道这意味着什么。这就是你在运行时获得NoSuchMethodException的原因。

你还应该删除返回null的findViewById方法 - 除了问题之外什么都不会造成。

不要实现OnClickListener,这不是你想要的。然后,摆脱你的onClick方法。相反,您应该为每个ImageButton定义一个新的OnClickListener。像这样:

play.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        Log.i("play","play button clicked");
    }
});

为“play”,“detail”和“buy”ImageButtons中的每一个执行此操作。请注意,您可以使用定义为final的任何变量。

final Product product = (Product)getItem(position);

detail.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        Log.i("detail", "clicked for product " + product.toString());
    }
}

我希望指出你正确的方向。

答案 3 :(得分:0)

我做了类似的事情,但是在主要活动中(宣布了listview)我添加了这个:

listView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
              switch(view.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;  
               }     
    });

答案 4 :(得分:0)

如果您的布局在Fragment中夸大了,您想要捕获XML指定的onClick,则可能会将其定向到您的主Activity

我有一个显示许多片段的ViewPager。每个片段都有一个可点击的ImageView,点击时应调用changePicture,但不会调用片段代码,它是接收调用的主Activity

所以我解决它的方法是定义许多人提到的onClickListener