0

我想設置自定義ListViewArrayAdapter。每個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 

標誌着我在這在代碼中發生錯誤的行。這可能是什麼原因?

回答

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

的更換

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); 
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()); 

它會正常工作。其他視圖是相似的。

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; 
    } 
} 
相關問題