Mam widok listy w mojej aplikacji z przyciskami obrazu i piszę akcję dla każdego przycisku obrazu. ale otrzymuję wyjątek zerowego wskaźnika po dotknięciu przycisków obrazu. nie znam powodu. proszę pomóż mi.
Mój kod:
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);
}
}
W klasie 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;
}
}
W Inventor_list.xml:
<ListView android:id="@+id/listExample"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
W 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 odpowiedzi
Umieściłeś differant xml w klasie InventoryListActivity, a inventry_list_item.xml dat meas muszę napompować ten składnik widoku w tej klasie, a następnie po u można użyć tych widoku w swojej klasie.
Prawdopodobnie system nie może znaleźć widoku, na który wskazujesz.
Upewnij się, że jedyny raz, kiedy dzwonisz do R.id.foobar, jest w nadpisaniu onActivityStarted. Gwarantuje to, że nie otrzymasz tego błędu.
Mam nadzieję, że to pomogło.
Podobne pytania
Powiązane pytania
Nowe pytania
android
Android to mobilny system operacyjny Google, używany do programowania lub tworzenia urządzeń cyfrowych (smartfony, tablety, samochody, telewizory, Wear, Glass, IoT). W przypadku tematów związanych z Androidem użyj tagów specyficznych dla Androida, takich jak android-intent, android-activity, android-adapter itp. W przypadku pytań innych niż programowanie lub programowanie, ale związanych ze strukturą Androida, użyj tego linku: https: // android.stackexchange.com.