带有图像和数组文本的Android Listview

时间:2013-11-09 18:11:22

标签: android listview android-listview

我想使用从(string.xml)和图像中获取的文本填充列表视图。 我这样做了,但我得到了NPE 你能帮助我吗? 感谢

public class Menu_activity extends ListActivity {
int[] img = { R.drawable.01, R.drawable.1, R.drawable.2,
        R.drawable.3, R.drawable.4,
        R.drawable.5};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getListView().setDividerHeight(1);
    getListView().setAdapter(new BindDataAdapter(this, img, men));

}
private String[] men = getResources().getStringArray(R.array.men);

1 个答案:

答案 0 :(得分:2)

更改为

private String[] men;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    men = getResources().getStringArray(R.array.men); // inside onCreate
    getListView().setDividerHeight(1);
    getListView().setAdapter(new BindDataAdapter(this, img, men));     
}