在我的应用程序中,我在 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/