我正面臨一個問題,我明白但無法找到解決方案。因此 - 我有一個listview和一些項目。我想更改背景顏色項目,這完美的作品,但是當項目小於屏幕的大小。當項目更多,我想改變一個項目的背景顏色是不可觀看的應用程序崩潰。 下面是一些代碼:當試圖更改項目背景顏色時,Listview崩潰應用程序
btnRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setItemRed(v);
}
});
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listData);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String name = adapterView.getItemAtPosition(i).toString();
Cursor data = myDB.getItemID(name);
int itemID = -1;
while(data.moveToNext()){
itemID = data.getInt(0);
}
if(itemID > -1){
Intent intent = new Intent(ViewElevators.this, ElevatorInfo.class);
intent.putExtra("id",itemID);
intent.putExtra("name",name);
startActivity(intent);
}
else{
toastMessage("No ID associated with that name");
}
}
});
而且setItemRed(); :
public void setItemRed(View view) {
for (int i = 0; i <= myDB.getLastID(); i++) {
String date = myDB.getcol3(i);
if (date.equals("null")) {
} else {
String day = date.substring(0, 2);
String dayp = date.substring(0, 1);
int dayp1 = Integer.parseInt(dayp);
int day1 = 0;
if (dayp1 == 0) {
String dayone = date.substring(1, 2);
day1 = Integer.parseInt(dayone);
} else {
day1 = Integer.parseInt(day);
}
String currentDate = currentDate();
String currentDay = currentDate.substring(0, 2);
int currentDay1 = Integer.parseInt(currentDay);
if (date.length() == 10) {
if (day1 < currentDay1) {
listView.getChildAt(i).setBackgroundColor(Color.RED);
}
}
}
的logcat:
E/AndroidRuntime(3486):
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.setBackgroundColor(int)' on a null object reference
我已閱讀並嘗試了很多東西,但沒有helped.Also,我相信這個問題來自屏幕旋轉或列表視圖具有存儲位置在一個布爾值爲ie的情況下記住它們。 非常感謝您的幫助!
這似乎是合乎邏輯的。所以getChildAt()可以獲得屏幕配給中的位置嗎?我會嘗試以其他方式做,謝謝! – Goshoy