ListAdapter中的空指针异常

时间:2011-08-18 07:24:16

标签: android

我想在列表视图中显示数据,但我在行

中得到了错误空指针异常运行时间
fList.setAdapter(la);

我的代码在这里列表是在日志中完成打印 感谢

public class Forfattere extends Activity {
/** Called when the activity is first created. */
ArrayList<String> aList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.forfattere);
    ListView fList = (ListView)findViewById(R.id.Author_list);
    ArrayList<String> a = DBlistbookfestival.getAuthorName();
    for(int i=0;i<a.size();i++){
        aList.add(a.get(i));
        Log.v("all_data",a.get(i));
    }
    ListAdapter1 la = new ListAdapter1(this, R.id.Author_list, aList);
    fList.setAdapter(la);
 }

private class ListAdapter1 extends ArrayAdapter<String> {  // --CloneChangeRequired
    private ArrayList<String> mList;  // --CloneChangeRequired
    public ListAdapter1(Context context, int textViewResourceId,ArrayList<String> list) { // --CloneChangeRequired
        super(context, textViewResourceId, list);
        this.mList = list;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        View view = convertView;
        try{
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.custom_radio, null);     // --CloneChangeRequired(list_item)
        }
        final String listItem = mList.get(position);    // --CloneChangeRequired                
        if (listItem != null) {
            ( (TextView) view.findViewById(R.id.textView111) ).setText(listItem);
        }}catch(Exception e){

        }
        return view;
    }
}   

}

3 个答案:

答案 0 :(得分:2)

立即尝试:

public class Forfattere extends Activity {
/** Called when the activity is first created. */
ArrayList<String> aList = new ArrayList<String>();
ListAdapter1 la = null;
ListView fList = null;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.forfattere);
    fList = (ListView)findViewById(R.id.Author_list);
    ArrayList<String> a = DBlistbookfestival.getAuthorName();
    for(int i=0;i<a.size();i++){
        aList.add(a.get(i));
        Log.v("all_data",a.get(i));
    }
    la = new ListAdapter1(this,android.R.layout.simple_list_item_1, aList);
    fList.setAdapter(la);
 }

private class ListAdapter1 extends ArrayAdapter<String> {  // --CloneChangeRequired
    private ArrayList<String> mList;  // --CloneChangeRequired
    public ListAdapter1(Context context, int textViewResourceId,ArrayList<String> list) { // --CloneChangeRequired
        super(context, textViewResourceId, list);
        this.mList = list;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        View view = convertView;
        try{
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.custom_radio, null);     // --CloneChangeRequired(list_item)
        }
        final String listItem = mList.get(position);    // --CloneChangeRequired                
        if (listItem != null) {
            ( (TextView) view.findViewById(R.id.textView111) ).setText(listItem);
        }}catch(Exception e){

        }
        return view;
    }
}   
}

在下面我使用了android默认列表项,因为你没有声明一个。

la = new ListAdapter1(this,android.R.layout.simple_list_item_1, aList);

答案 1 :(得分:1)

你的问题来自这条线:

ListView fList = (ListView)findViewById(R.id.Author_list);

findviewbyid返回null,这意味着您的布局R.layout.forfattere中没有带有ID为R.id.Author_list的元素。 查看您的布局以查看列表视图是否具有此ID。

答案 2 :(得分:0)

如果findbyId在布局中找不到你的列表视图R.id.Author_list,你的listview可能为null。