nullpointer当simperecursoradapter中的findviewbyid()何时
-
24-10-2019 - |
题
我使用的SimpleCursorAdapter带有XML文件,其中定义了一些视图:
<LinearLayout ...>
<ImageView android:id="@+id/listIcon" />
<TextView android:id="@+id/listText" />
</LinearLayout>
我的目的是通过编程方式设置文本视图的文本颜色,以及线性layout的背景颜色(即列表视图中的每一行);颜色是从数据库返回的。
例如,在尝试操纵文本视图时,我发现了NPE,但在没有任何抱怨的情况下找到了NPE:
TextView tv = (TextView) findViewById(R.id.listText);
tv.setTextColor(color); // NPE on this line
这是公平的;如果列表中有多个条目,则可以合理地假设“r.id.listtext“将行不通。所以我扩展了SimpleCursor适配器:
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
TextView text = (TextView) row.findViewById(R.id.listText);
// ImageView icon = (ImageView) row.findViewById(R.id.listIcon);
// If there's an icon defined
if (mIcon_id != 0) {
// icon.setImageResource(mIcon_id);
}
// If text color defined
if (mTextColor != 0) {
text.setTextColor(mTextColor);
}
// If background color set
if (mBackgroundColor != 0) {
row.setBackgroundColor(mBackgroundColor);
}
return(row);
}
我得到了两个不同的错误:
- 类似的NPE也被抛出了text.settextcolor(mtextcolor)"
- 如果带有图像视图的行不受写,我会得到一个”ClassCastException:Android.widget.TextView“我在哪里打电话”row.findviewbyid(r.id.listicon)"
作为参考,我试图使用Commonsware的示例代码,将其应用于我的情况。 链接(PDF)
更改为:
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
if (convertView == null) convertView = View.inflate(mContext, R.layout.theme_item, null);
TextView text = (TextView) convertView.findViewById(R.id.listText_tv);
ImageView icon = (ImageView) convertView.findViewById(R.id.listIcon_iv);
// If there's an icon defined
if (mIcon_id != 0) {
icon.setImageResource(mIcon_id);
}
// If text color defined
if (mTextColor != 0) {
text.setTextColor(mTextColor);
}
// If background color set
if (mBackgroundColor != 0) {
convertView.setBackgroundColor(mBackgroundColor);
}
bindView(convertView, mContext, mCursor);
return(convertView);
}
现在,我在下一个活动中获得了ClassCastException(单击列表项)。下一个活动中没有任何修改。在使用条目的列表中使用SimpleListAdapter(点击会导致Activity2)时,它可以工作,因此我认为这仍然是我在这个扩展类中做错的事情。
解决方案
没错 convertview 将永远是现有实例;您应该检查它是否为null,然后进行实例化。如果没有,您可以像这样更改它。
这应该是:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
convertView = //inflate your row here
View row = convertView;
//Manipulate the row here
return(row);
}
其他提示
我会修改GetView方法:
public View getView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(getContext(), R.layout.myLayout, null);
TextView text = (TextView) convertView.findViewById(R.id.listText);
ImageView icon = (ImageView) convertView.findViewById(R.id.listIcon);
// If there's an icon defined
if (mIcon_id != 0) {
icon.setImageResource(mIcon_id);
}
// If text color defined
if (mTextColor != 0) {
text.setTextColor(mTextColor);
}
// If background color set
if (mBackgroundColor != 0) {
convertView.setBackgroundColor(mBackgroundColor);
}
return convertView;
}
我认为您正在获得NPE,因为您正在尝试在他们不存在的视图中创建一个文本视图和imageview。
当您想将listView带有来自数据库的条目时,在您的活动中,您可以定义main.xml listView:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1">
</ListView>
在on创作方法中,您将视图设置为此xml setContentView(R.layout.main);
. 。然后,您将光标创建到数据库和自定义适配器:
MySimpleCursorAdapter adapter = new MySimpleCursorAdapter(this, R.layout.entry,
names, new String[] {Phones.NAME, Phones.NUMBER}, new int[] {
R.id.listIcon, R.id.listText});
startManagingCursor(cursor);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
然后,您可以使用listicon和listText定义一个entry.xml,适配器点。在我的示例中,我正在查询联系人列表的名称和号码。
在您的自定义适配器中,您应该在GetView或BindView中访问文本视图和ImageView,没有任何问题。
这里 您有示例可以在联系人列表中获得其图片,名称和号码的所有联系人,但是使用ListActivity而不是活动,只有一个XML,带有两个文本视图和一个ImageView。如果使用listActivity,则无需使用listView,也无需在活动中设置内容视图。
我希望它有帮助!
不要忘记放置:每个视图的Layout_width和Layout_heigth。