如果你喜欢的是不是目前在ListView然后getChildAt(可视)将返回null。
您遍历在列表视图中的所有项目,我的猜测是,它拥有更多的项目过多,无法在屏幕上。当你最喜欢的物品是其中的一个,那么这段代码
listview.getChildAt(i-listview.getFirstVisiblePosition())
将返回null。当你调用findViewById(R.id.item_name)时,会导致NullPointerException。
刚上getChildAt的结果添加一个检查空()。如果它为null,则不执行任何操作,如果它非空,则调用第二部分。当您最喜欢的物品不在屏幕上时,这可以防止出现异常情况,并且可以在屏幕上显示您喜欢的物品时将其变为红色。
更新
我的道歉,我读快速,误解了你的问题是关于NullPointerException异常,但你说,当你从一个按钮单击处理程序调用它的代码工作正常,但不是当你调用它在启动时自动启动。
你说得对,当你还在onCreate()时,ListView还没有加载任何项目。您可以在运行代码之前添加延迟。对我来说,以下工作:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// initialize the ListView with data for the list items. (I'm using a string array in this
// example. You are loading it from a database table, but that is the same in principle.)
ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.item_list, R.id.item_name, Items);
ListView listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adapter);
// ask the system to wait before setting the background color of the favorite item so that
// the ListView has time to load the items.
final int DELAY_IN_MILLISECONDS = 100;
listview.postDelayed(new Runnable() {
@Override
public void run() {
ChangeMyFavoritesBG();
}
}, DELAY_IN_MILLISECONDS);
}
正如你可以在上面的例子中看到的,初始化的ListView之后,你问的系统调用ChangeMyFavoritesBG()
之前要等待100毫秒。希望有足够的时间将数据库中的项目加载到ListView中。如果时间不够,那么你当然可以使用更长的延迟。
替代
以上应该工作,但说实话,我不会写这种方式。上面的代码非常脆弱,因为它取决于加载项目需要多长时间。我建议你把你的背景色添加到自定义的适配器中。
因为您希望以自定义的方式显示项目 - 您希望当它是最喜欢的一个时它们具有红色背景 - 您应该使用自定义适配器。重写bindView()函数以在背景为最喜欢的背景时使背景变成红色,或者当它不是最喜欢的背景时使背景变为正常背景。
我不知道当前如何从数据库中将项目获取到ListView中,但从SimpleCursorAdaptor继承将工作得很好。
public class FavoritesItemAdapter extends SimpleCursorAdapter {
public FavoritesItemAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
// read the name from the database
int nameColumnIndex = cursor.getColumnIndexOrThrow("name");
String name = cursor.getString(nameColumnIndex);
// write the name to the TextView
TextView nameText = (TextView) view.findViewById(R.id.item_name);
nameText.setText(name);
// set the background to normal or to red, depending on if it is the favorite one or not
boolean isFavorite = db_verification(name);
if (isFavorite) {
nameText.setBackgroundResource(R.drawable.redcolor);
} else {
nameText.setBackgroundResource(android.R.color.transparent);
}
}
public boolean db_verification(String name) {
// this is a stub. You must use your own code here
return name.equals("the favorite one");
}
}
然后你可以扔掉ChangeMyFavoritesBG()
并与的onCreate适配器()这样初始化的ListView。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Cursor cursor = readItemsFromDatabase();
String[] from = new String[] { "name_column" }; // mapping from database column name ...
int[] to = new int[] { R.id.item_name }; // ... to View ID in the item's layout.
FavoritesItemAdapter adapter = new FavoritesItemAdapter(this, R.layout.item_list, cursor, from, to, 0);
ListView listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adapter);
}
祝你好运!
你能给我示例代码吗?请吗?,谢谢 – SamAyoub95 2014-10-07 20:16:41