求助大神!android的baseadapter老是报空指针

package com.example.kaoqin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ObjectUtils.Null;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class User_TiaoZhuan_Activity2 extends Activity
{
private ListView list;
private SimpleAdapter SAda;
public String huoquuser;
private String[] From = new String[]{"yonghu","kaishi","jieshu","zong","yuanyin","zhuangtai","id"};
public ArrayList> data = new ArrayList>();
private ArrayList datalist = new ArrayList();
public String NAME;
public String START;
public String END;
public String SUM;
public String REASON;
public String STATE;
public String ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.user_tiaozhuan_activity2);
list = (ListView)findViewById(R.id.listViewuser);
Intent intent = getIntent();//将检索出来的intent赋值给一个Intent类型的变量intent,getIntent()方法用于直接返回Intent实例
Bundle bundle = intent.getExtras();///获取前面的那个activity传过来的数据
huoquuser = bundle.getString("username");

    HttpClien httpClien = new HttpClien();
    data = httpClien.HTTPLeaveListView(huoquuser, "2");

    //SAda = new SimpleAdapter(User_TiaoZhuan_Activity2.this, getdata(data),R.layout.user_tiaozhuan_buju2, From,new int[]{R.id.tiaozhuan_user2,
        //R.id.tiaozhuan_start2,R.id.tiaozhuan_end2,R.id.tiaozhuan_sum2,R.id.tiaozhuan_reason2,R.id.tiaozhuan_shenhe2,R.id.tiaozhuan_id2});
    //list.setAdapter(SAda);
      Edit nihao = new Edit(this, getdata(data));//把数据源放入baseadapter
      list.setAdapter(nihao);

}
public ArrayList<ItemBean> getdata(ArrayList<HashMap<String,Object>> shuju)
{
    for (int i = 0; i < data.size(); i++) 
    {
        NAME = String.valueOf(shuju.get(i).get("name"));
        START = String.valueOf(shuju.get(i).get("start"));
        END = String.valueOf(shuju.get(i).get("end"));
        SUM = String.valueOf(shuju.get(i).get("sum"));
        REASON = String.valueOf(shuju.get(i).get("reason"));
        STATE = String.valueOf(shuju.get(i).get("state"));
        ID = String.valueOf(shuju.get(i).get("id"));
        ItemBean it = new ItemBean(NAME, START, END, SUM, REASON, STATE,ID);
        datalist.add(it);
    }
    return datalist;
}

}
class Edit extends BaseAdapter
{
private Context context;
private ArrayList list;
private LayoutInflater miInflater;
public Edit(Context context,ArrayList list)
{
LayoutInflater miInflater;
miInflater = LayoutInflater.from(context);
this.context = context;
this.list = list;
//根据context上下文加载布局,这里的是User_TiaoZhuan_Activity本身,即this
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    //在此适配器中所代表的数据集中的条目数 
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    //获取数据集中与指定索引对应的数据项 
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    //获取在列表中与指定索引对应的行id 
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ViewHolder kongjian = null;
    if(convertView == null)
    {
        convertView = miInflater.inflate(R.layout.user_tiaozhuan_buju2, null);
        kongjian = new ViewHolder();
        kongjian.TV_sure = (TextView)convertView.findViewById(R.id.yunxu);
        kongjian.TV_cancel = (TextView)convertView.findViewById(R.id.bohui);
        kongjian.TVuser = (TextView)convertView.findViewById(R.id.tiaozhuan_user2);
        kongjian.TVstart = (TextView)convertView.findViewById(R.id.tiaozhuan_start2);
        kongjian.TVend = (TextView)convertView.findViewById(R.id.tiaozhuan_end2);
        kongjian.TVsum = (TextView)convertView.findViewById(R.id.tiaozhuan_sum2);
        kongjian.TVreason = (TextView)convertView.findViewById(R.id.tiaozhuan_reason2);
        kongjian.TVstate = (TextView)convertView.findViewById(R.id.tiaozhuan_shenhe2);
        kongjian.TVid = (TextView)convertView.findViewById(R.id.tiaozhuan_id2);
        //将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag 
        convertView.setTag(kongjian);
    }else 
    {
        kongjian = (ViewHolder)convertView.getTag();
    }
    kongjian.TV_sure.setText("允许请假");
    kongjian.TV_cancel.setText("驳回请假");
    ItemBean bean = list.get(position);
    kongjian.TVuser.setText(bean.Itemuser);;
    kongjian.TVstart.setText(bean.Itemstarttime);
    kongjian.TVend.setText(bean.Itemendtime);
    kongjian.TVsum.setText(bean.Itemsumtime);
    kongjian.TVreason.setText(bean.Itemreason);
    kongjian.TVstate.setText(bean.Itemstate);
    kongjian.TVid.setText(bean.ItemId);
    kongjian.TV_sure.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("通过");
        }
    });
    kongjian.TV_cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("不通过");
        }
    });
    return convertView;
}

}
class ViewHolder
{
TextView TV_sure;
TextView TV_cancel;
TextView TVuser;
TextView TVstart;
TextView TVend;
TextView TVsum;
TextView TVreason;
TextView TVid;
TextView TVstate;

}
class ItemBean
{
public String Itemuser;
public String Itemstarttime;
public String Itemendtime;
public String Itemsumtime;
public String Itemreason;
public String Itemstate;
public String ItemId;
public ItemBean(String a,String b,String c, String d,String e,String f,String g)
{
Itemuser = a;
Itemstarttime = b;
Itemendtime = c;
Itemsumtime = d;
Itemreason = e;
Itemstate = f;
ItemId = g;
}
}图片说明
错误显示的是121行存在空指针,inflate里的布局文件是存在的啊,R类里也有
地址啊(R.layout.user_tiaozhuan_buju2, null);

1个回答

应该是miInflater 为空,private Context context;
private ArrayList list;
private LayoutInflater miInflater;
public Edit(Context context,ArrayList list)
{
LayoutInflater miInflater;
miInflater = LayoutInflater.from(context);
this.context = context;

这块有问题,adapter里的context不要与传进来的参数同名,你的代码写的真的是。。。。。。。。

u014062218
耶梦萨冷 回复ImportSUC: 大神,照着改还是报空指针了 convertView = miInflater.inflate(R.layout.user_tiaozhuan_buju2, null); 提示还是跳到这一行
3 年多之前 回复
yiranhaiziqi
ImportSUC 抛开代码规范,其实关键原因应该是miInflater = LayoutInflater.from(context); this.context = context;这两句,顺序倒过来就可以
3 年多之前 回复
yiranhaiziqi
ImportSUC 回复耶梦萨冷:private Context mCntext; private ArrayList list; private LayoutInflater miInflater; public Edit(Context context,ArrayList list) { this.mCntext = context; LayoutInflater miInflater = LayoutInflater.from(mCntext);
3 年多之前 回复
u014062218
耶梦萨冷 请问大神要怎么修改?
3 年多之前 回复
u014062218
耶梦萨冷 让大神见笑了,小弟菜鸟一只初学安卓。。。可以恳请大神略微详细地解释下吗?为什么参数不能同名?
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问