定制适配器用于自定义列表视图空指针异常

时间:2015-04-15 14:27:22

标签: java android parse-platform nullpointerexception custom-adapter

我的自定义适配器类中出现Null指针异常。我不明白为什么。它在holder.name.setText(s.getName())中给了我一个例外。请帮忙。我必须提交我的项目。另外,请解释适配器的工作原理以及何时使用ArrayAdapter,BaseAdapter等适配器。如果我有一个像我这样的类型的列表怎么办。什么更有益? customadapter.java类。

public class CustomAdapter extends BaseAdapter{
    private Context activity;
    private LayoutInflater inflater = null;
    private List<Students> student;
    int layout;

    public CustomAdapter(Context activity, List<Students> Student) {
        this.activity = activity;
        this.student = Student;
        inflater = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {
        return student.size();
    }

    @Override
    public Object getItem(int position) {
        return student.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public  class ViewHolder {
        //ImageView imageView ;
        TextView name  ;
        TextView usn ;
        TextView semester  ;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        View v =view;
        ViewHolder holder = new ViewHolder();

        if (view == null) {
            LayoutInflater li = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(android.R.layout.simple_list_item_2,parent,false);
            holder.name = (TextView)v.findViewById(R.id.name);
            holder.usn = (TextView)v.findViewById(R.id.usn);
            holder.semester = (TextView)v.findViewById(R.id.semester);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        Students s = new Students();
        s = student.get(position);
        holder.name.setText(s.getName());
        holder.usn.setText(s.getUSN());
        holder.semester.setText(s.getSemester());

        Log.d("CustomAdapter.class", "CustomAdapter");
       // imageView.setImageDrawable(s.getPic());
        return v;
      }
    }

我的主要活动。

lv = (ListView)findViewById(R.id.listView);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Students");
query.findInBackground(new FindCallback<ParseObject>() {
    @Override
    public void done(List<ParseObject> parseObjects, ParseException e) {
        if(e==null) {

            for(ParseObject ob: parseObjects) {
                List<Students> studentsList= new ArrayList<Students>();
                s= new Students();
                String a = ob.getString("Name").toString();
                Log.d(TAG, a);
                s.setName(a);

                Log.d(TAG, s.Name);

                s.setUSN(ob.getString("USN").toString());

               // s.setPic(ob.getBytes());
                s.setSemester(ob.getString("Semester").toString());
                studentsList.add(s);

                adapter = new CustomAdapter(Main2Activity.this, studentsList);
                lv.setAdapter(adapter);
            }
        } else {
            Toast.makeText(Main2Activity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }
});

这是我调用适配器的代码段。 list_item.xml布局文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.redux.kumardivyarajat.parsedemo.Main2Activity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/name"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/usn"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/name"
        android:layout_toEndOf="@+id/name"
        android:layout_marginLeft="203dp"
        android:layout_marginStart="203dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/semester"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    </RelativeLayout>

mainactivity2.xml。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.redux.kumardivyarajat.parsedemo.Main2Activity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    </RelativeLayout>

这是我的数据模型。

public class Students {

String Name, USN, Semester;
Drawable pic;

Students () {

};

public Students(String name, Drawable pic, String semester, String USN) {
    Name = name;
    this.pic = pic;
    Semester = semester;
    this.USN = USN;
}


public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public Drawable getPic() {
    return pic;
}

public void setPic(Drawable pic) {
    this.pic = pic;
}

public String getSemester() {
    return Semester;
}

public void setSemester(String semester) {
    Semester = semester;
}

public String getUSN() {
    return USN;
}

public void setUSN(String USN) {
    this.USN = USN;
 }
}

2 个答案:

答案 0 :(得分:1)

看来你正在使用android资源布局,因为包是(android.R。*)。如果要使用自己的xml资源,请更改行:

v = li.inflate(R.layout.your_xml_name,parent,false);

答案 1 :(得分:0)

不要忘记初始化ArrayList

变化,

private List<Students> student;

private List<Students> student = new List<Students>;
CustomAdapter.class中的