我想設置自定義ListView
和ArrayAdapter
。每個ListItem
有一個ImageView
和兩個TextViews
。設置自定義ListView圖像時出錯
每個項目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
標誌着我在這在代碼中發生錯誤的行。這可能是什麼原因?