您好我的listivew有问题。请检查下面的代码:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
String selected;
//selected = parent.getItemAtPosition(position).toString();
selected = parent.getItemAtPosition(lv.getFirstVisiblePosition() + position).toString();
if( selected.equals("Apple") ){
Intent apple = new Intent(Fruits.this, Apples.class);
startActivity(apple);
}
else if( selected.equals("Apricot") ){
Intent apricot = new Intent(Fruits.this, Apricots.class);
startActivity(apricot);
}
else if( selected.equals("Avocado") ){
Intent avocado = new Intent(Fruits.this, Avocado.class);
startActivity(avocado);
}
else if( selected.equals("Banana") ){
Intent banana = new Intent(Fruits.this, Banana.class);
startActivity(banana);
}
else if( selected.equals("Blackberry") ){
Intent blackberry = new Intent(Fruits.this, Blackberries.class);
startActivity( blackberry );
}
else if( selected.equals("Blueberry") ){
Intent blueberry = new Intent(Fruits.this, Blueberry.class);
startActivity(blueberry);
}
else if( selected.equals("Cranberry") ){
Intent cranberry = new Intent(Fruits.this, Cranberry.class);
startActivity(cranberry);
}
else if( selected.equals("Figs") ){
Intent fig = new Intent(Fruits.this, Figs.class);
startActivity(fig);
}
else if( selected.equals("Grapefruit") ){
Intent grapefruit = new Intent(Fruits.this, Grapefruit.class);
startActivity(grapefruit);
}
else if( selected.equals("Kiwi") ){
Intent kiwi = new Intent(Fruits.this, Kiwi.class);
startActivity(kiwi);
}
else if( selected.equals("Lemon") ){
Intent lemon = new Intent(Fruits.this, Lemons.class);
startActivity(lemon);
}
else if( selected.equals("Melon") ){
Intent melon = new Intent(Fruits.this, Melon.class);
startActivity(melon);
}
else if( selected.equals("Orange") ){
Intent orange = new Intent(Fruits.this, Orange.class);
startActivity(orange);
}
else if( selected.equals("Papaya") ){
Intent papaya = new Intent(Fruits.this, Papaya.class);
startActivity(papaya);
}
else if( selected.equals("Peach") ){
Intent peach = new Intent(Fruits.this, Peaches.class);
startActivity(peach);
}
else if( selected.equals("Pear") ){
Intent pear = new Intent(Fruits.this, Pears.class);
startActivity(pear);
}
else if( selected.equals("Pineapple") ){
Intent pineapple = new Intent(Fruits.this, Pineapple.class);
startActivity(pineapple);
}
else if( selected.equals("Plum") ){
Intent plum = new Intent(Fruits.this, Plum.class);
startActivity(plum);
}
else if( selected.equals("Prune") ){
Intent prune = new Intent(Fruits.this, Prunes.class);
startActivity(prune);
}
else if( selected.equals("Raspberry") ){
Intent raspberry = new Intent(Fruits.this, Raspberry.class);
startActivity(raspberry);
}
else if( selected.equals("Rhubarb") ){
Intent rhubard = new Intent(Fruits.this, Rhubard.class);
startActivity(rhubard);
}
else if( selected.equals("Strawberry") ){
Intent strawberry = new Intent(Fruits.this, Strawberries.class);
startActivity(strawberry);
}
} // end of OnItemClick method
final String[] classes = new String[] {
"Apple", // #0
"Apricot", // #1
"Avocado", // #2
"Banana", // #3
"Blackberry", // #4
"Blueberry", // #5
"Cranberry", // #6
"Figs", // #7
"Grapefruit", // #8
"Kiwi", // #9
"Lemon", // #10
"Melon", // #11
"Orange", // #12
"Papaya", // #13
"Peach", // #14
"Pear", // #15
"Pineapple", // #16
"Plum", // #17
"Prune", // #18
"Raspberry", // #19
"Rhubarb", // #20
"Strawberry" // #21
};
每当我运行应用程序时,它都会在此行中抛出nullpointerexception:
selected = parent.getItemAtPosition(lv.getFirstVisiblePosition() + position).toString();
我的代码出了什么问题?真的很感激任何帮助。感谢。
答案 0 :(得分:0)
你是对的,错误代码在行中:
selected = parent.getItemAtPosition(lv.getFirstVisiblePosition()+position).toString();
由于功能:getFirstVisiblePosition() + position
,有时会大于parent
的大小,您的代码将变为:null.toString()
,当然还有NullPointerException
。您可以在Link to AdapterView here
getFirstVisiblePosition
已编辑:我认为您不应再使用此行,只需使用:selected = classes[position]
。这将解决你的问题!