xiaoyan_12 2013-03-27 05:16 采纳率: 25%
浏览 5373
已采纳

ListAdapter 的空指针异常

我想在List View中显示数据,但是在运行时获得Null Pointer Exception错误。

fList.setAdapter(la);

下面是我用的代码。list 在 Log 中全部打印出来了。

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;
    }
}   

如何出现的这个异常啊?

  • 写回答

2条回答 默认 最新

  • yongyong_21 2013-04-02 03:37
    关注

    使用这个方法:

    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;
        }
    }   
    }
    

    如果没有申明 list item,请使用android 中默认的。

    la = new ListAdapter1(this,android.R.layout.simple_list_item_1, aList);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条