问题
i have a list view in my app with image buttons and i write action for each image button. but i get null pointer exception when touch the image buttons. i do not know the reason. please help me.
my code:
public class InventoryListActivity extends ListActivity {
private InventoryAdapter adapter;
private InventoryObserver inventoryObserver;
ListView listview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory_list);
if(!IAPManager.isBillingSupported())
showDialog(1);
listview = (ListView)findViewById(R.id.listExample);
adapter = new InventoryAdapter(this); // here i call InventoryAdapter class for list view the items.
setListAdapter(adapter);
inventoryObserver = new InventoryObserver();
IAPManager.shared().getInventory().addObserver(inventoryObserver);
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;
}
}
});
}
@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
Log.e("position",""+pos);
}
}
in InventoryAdapter class:
public class InventoryAdapter extends BaseAdapter implements Observer{
public InventoryAdapter(Context ctx) {
context = ctx;
inventory = IAPManager.shared().getInventory();
inventory.addObserver(this);
inventory.load();
}
.......
@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;
}
..........
return view;
}
}
in inventory_list.xml:
<ListView android:id="@+id/listExample"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
in Inventry_list_item.xml:
......
<ImageButton android:background="@drawable/play_btn" android:focusable="false" android:onClick="onItemClick"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/imageButton2" android:layout_weight="0.5" />
......
Log cat:
ERROR/AndroidRuntime(2038): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(2038): at com.google.iap.BillingService.handleCommand(Unknown Source)
ERROR/AndroidRuntime(2038): at com.google.iap.BillingService.onStart(Unknown Source)
ERROR/AndroidRuntime(2038): at android.app.Service.onStartCommand(Service.java:306)
ERROR/AndroidRuntime(2038): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2873)
回答1:
you have put the differant xml in the InventoryListActivity class rather the Inventry_list_item.xml dat meas i need to inflate this view component in this class then after u can user those view in your class .
回答2:
What is likely happening is the system can't find the View you are pointing at.
Make sure the only time you call R.id.foobar is in your onActivityStarted override. This ensures that you won't get this error.
Hope this helped.
来源:https://stackoverflow.com/questions/6393997/null-pointer-exception-on-set-on-click-listener-in-list-view-on-android