在Android列表视图上一套空指针异常的点击监听器 [英] null pointer exception on set on click listener in list view on android
问题描述
我在我的图像按钮的应用程序列表视图和我写的每个图像按钮操作。
但触摸图像按钮,当我得到空指针异常。我不知道原因。请帮助我。
我的code:
公共类InventoryListActivity扩展ListActivity {私人InventoryAdapter适配器;
私人InventoryObserver inventoryObserver;
ListView控件列表视图;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.inventory_list); 如果(!IAPManager.isBillingSupported())
的ShowDialog(1); 列表视图=(ListView控件)findViewById(R.id.listExample); 适配器=新InventoryAdapter(本); //这里我呼吁名单InventoryAdapter类查看的项目。
setListAdapter(适配器); inventoryObserver =新InventoryObserver();
IAPManager.shared()getInventory()的addObserver(inventoryObserver)。 listview.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>适配器视图,视图观点,INT位置,长的id){
开关(view.getId()){
案例R.id.imageButton1:
Log.e(买入,买入位置+位置);
打破;
案例R.id.imageButton2:
Log.e(玩,播放位置+位置);
打破;
案例R.id.imageButton3:
Log.e(详细信息,详细位置+位置);
打破;
}
}
});
}@覆盖
公共无效onListItemClick(ListView中升,视图V,诠释POS,长ID){
Log.e(位置,+正);
}
}
在InventoryAdapter类:
公共类InventoryAdapter扩展BaseAdapter实现观测{公共InventoryAdapter(上下文CTX){
上下文= CTX;
库存= IAPManager.shared()getInventory()。
inventory.addObserver(本);
inventory.load();
}
.......
@覆盖
公众诠释的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;
}
..........
返回视图。
}
}
在inventory_list.xml:
< ListView的机器人:ID =@ + ID / listExample
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/>
在Inventry_list_item.xml:
......
< ImageButton的机器人:背景=@绘制/ play_btn机器人:可聚焦=false的机器人:的onClick =onItemClick
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / imageButton2机器人:layout_weight =0.5/>
......
登录猫:
ERROR / AndroidRuntime(2038):显示java.lang.NullPointerException:产生的原因
ERROR / AndroidRuntime(2038):在com.google.iap.BillingService.handleCommand(来源不明)
ERROR / AndroidRuntime(2038):在com.google.iap.BillingService.onStart(来源不明)
ERROR / AndroidRuntime(2038):在android.app.Service.onStartCommand(Service.java:306)
ERROR / AndroidRuntime(2038):在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2873)
您已经把歧XML在InventoryListActivity类,而在Inventry_list_item.xml DAT MEAS我需要充气该类这个视图组件再经过U可以与用户鉴于在你的类。
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)
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 .
这篇关于在Android列表视图上一套空指针异常的点击监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!