在android上的列表视图中单击监听器上设置的空指针异常

时间:2011-06-18 05:01:18

标签: android android-layout android-listview

我的应用程序中有一个列表视图,带有图像按钮,我为每个图像按钮编写动作。 但是当触摸图像按钮时我得到空指针异常。我不知道原因。请帮帮我。

我的代码:

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

}

InventoryAdapter类中的

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

}

inventory_list.xml中的

:        

 <ListView android:id="@+id/listExample"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>

在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)

2 个答案:

答案 0 :(得分:1)

你已经将不同的xml放在InventoryListActivity类而不是Inventry_list_item.xml dat meas中我需要在这个类中扩充这个视图组件然后你可以在你的类中使用这些视图。

答案 1 :(得分:0)

可能发生的是系统无法找到您指向的视图。

确保您在onActivityStarted覆盖中唯一一次调用R.id.foobar。这可以确保您不会收到此错误。

希望这会有所帮助。