我想使用ListView
设置自定义ArrayAdapter
。每个ListItem
都有一个ImageView
和两个TextViews
。
每个项目rowlayout.xml
的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:src="@drawable/logo" />
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/item_subTitle"
android:layout_toRightOf="@+id/item_icon"
android:text="Titel"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/item_subTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/item_icon"
android:layout_alignLeft="@+id/item_title"
android:layout_marginBottom="16dp"
android:text="Untertitel" />
</RelativeLayout>
这是我在徽章活动中设置ListView
的方法:
public class Badges extends ActionBarActivity{
List<Badge> myBadges = new ArrayList<Badge>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.badges_layout);
populateBadgeList();
populateListView();
}
private void populateBadgeList() {
myBadges.add(new Badge("Some Text", "Some further Text", R.drawable.badge1));
//and so on
}
private void populateListView() {
ArrayAdapter<Badge> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Badge> {
public MyListAdapter() {
super(Badges.this, R.layout.rowlayout, myBadges);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.rowlayout, parent, false);
}
Badge currentBadge = myBadges.get(position);
ImageView itemIcon = (ImageView) findViewById(R.id.item_icon);
//the error is on the following line:
itemIcon.setImageResource(currentBadge.getIconID());
TextView itemTitle = (TextView) findViewById(R.id.item_title);
itemTitle.setText(currentBadge.getTitle());
TextView itemSubtitle = (TextView) findViewById(R.id.item_subTitle);
itemSubtitle.setText(currentBadge.getSubtitle());
return itemView;
}
}
这是徽章课程:
public class Badge {
String title;
String subtitle;
int iconID;
public Badge(String title, String subtitle, int iconID) {
this.title = title;
this.subtitle = subtitle;
this.iconID = iconID;
}
public String getTitle() {
return title;
}
public String getSubtitle() {
return subtitle;
}
public int getIconID() {
return iconID;
}
}
我收到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
我在代码中标记了错误发生的行。这可能是什么原因?
答案 0 :(得分:1)
而不是使用
ImageView itemIcon = (ImageView) findViewById(R.id.item_icon);
TextView itemTitle = (TextView) findViewById(R.id.item_title);
TextView itemSubtitle = (TextView) findViewById(R.id.item_subTitle);
使用
ImageView itemIcon = (ImageView) itemView.findViewById(R.id.item_icon);
TextView itemTitle = (TextView) itemView.findViewById(R.id.item_title);
TextView itemSubtitle = (TextView)itemView.findViewById(R.id.item_subTitle);
答案 1 :(得分:1)
替换
ImageView itemIcon = (ImageView) findViewById(R.id.item_icon);
这个用
ImageView itemIcon = (ImageView) itemView.findViewById(R.id.item_icon);
和类似的
TextView itemTitle = (TextView) itemView.findViewById(R.id.item_title);
TextView itemSubtitle = (TextView) itemView.findViewById(R.id.item_subTitle);
带
TextView itemTitle = (TextView) findViewById(R.id.item_title);
TextView itemSubtitle = (TextView) findViewById(R.id.item_subTitle);
答案 2 :(得分:0)
您刚刚充气的视图未添加到您的活动的布局中。所以使用活动方法
ImageView itemIcon = (ImageView) findViewById(R.id.item_icon);
itemIcon.setImageResource(currentBadge.getIconID());
您无法找到具有特定ID的视图。相反,你应该使用
ImageView itemIcon = (ImageView) itemView.findViewById(R.id.item_icon);
itemIcon.setImageResource(currentBadge.getIconID());
它会正常工作。其他观点相似。
答案 3 :(得分:0)
我认为你的问题出在这一行:ArrayAdapter adapter = new MyListAdapter();
更改为:MyListAdapter adapter = new MyListAdapter();
此外,将项目视图放在findViewById方法的前面(ImageView itemIcon =(ImageView)itemview.findViewById(R.id.item_icon);)
我实际上建议推广您的适配器类,以便您可以将任何资源(行布局)和列表传递给它:)(但是,应用程序无需工作。)
这样的事情:
private class MyListAdapter extends ArrayAdapter<Badge> {
int resource;
public MyListAdapter(Context context, int resource, List<Badge> items) {
super(context, resource, items);
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null) {
itemView = getLayoutInflater().inflate(resource, parent, false);
}
Badge currentBadge = getItem(position);
ImageView itemIcon = (ImageView) itemview.findViewById(R.id.item_icon);
//the error is on the following line:
itemIcon.setImageResource(currentBadge.getIconID());
TextView itemTitle = (TextView) itemview.findViewById(R.id.item_title);
itemTitle.setText(currentBadge.getTitle());
TextView itemSubtitle = (TextView) itemview.findViewById(R.id.item_subTitle);
itemSubtitle.setText(currentBadge.getSubtitle());
return itemView;
}
}