Android Array到ListView:NullExceptionPointer

时间:2015-02-23 04:26:50

标签: android arrays listview nullpointerexception

我是Android新手,我正在尝试在列表视图上显示数组,但是我得到了这些代码的nullpointerexception。我尝试用吐司显示我的阵列,我认为它没有问题。我如何声明ArrayAdapter有什么问题吗?这是我的代码:

ListView lvnames;
ArrayList<String> arrnames = new ArrayList<String>();

    String [] name = new String [3];

    name [0] = "a";
    name [1] = "b";
    name [2] = "c";


    for (String complete : name){
        arrnames.add(complete);
    }

    lvnames.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arrnames));
    lvnames.setOnItemClickListener(this);


}


@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), " "+position, Toast.LENGTH_SHORT).show();
}
PS:我试过了 ListAdapter la1 = new ArrayAdapter [和] ArrayAdapter la1 = new ArrayAdapter,但这些都不起作用。

0 个答案:

没有答案