在android中使用自定义类时,Listview中的Nullpointer异常

时间:2012-02-15 18:15:54

标签: android listview object nullpointerexception

您好我正在尝试实现一个listview,它将通过TextView显示类的内容。但是,适配器似乎不会接受对象的值,因为它会抛出一个nullpointer异常。

这是我的自定义类

     public class SubjectClass {

String sName;
String sCode;
String teacher;
}

鉴于Belows是我的CUstomarrayadapter的代码

        public CustomAdap(Activity context, ArrayList<SubjectClass> names) {
    super(context, R.layout.listtempl, names);
    this.context = context;
    this.names = names;
}

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.listtempl, null, true);
    TextView textView1 = (TextView) rowView.findViewById(R.id.label1);
    TextView textView2 = (TextView) rowView.findViewById(R.id.label2);
    TextView textView3 = (TextView) rowView.findViewById(R.id.label3);
    SubjectClass sClass= (SubjectClass)getItem(position);
    String s1 = sClass.sName;
    String s2 = sClass.sCode;
    String s3 = sClass.teacher;
    Toast.makeText(getContext(), sClass.sCode,Toast.LENGTH_SHORT).show();
    textView1.setText(s1);
    textView2.setText(s2);
    textView3.setText(s3);

    return rowView;
}

这是适配器使用的xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
     <TextView android:text="@+id/label1" android:textColor="#F1235AFA"   android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/label" android:layout_alignParentTop="true" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_alignParentLeft="true"></TextView>
<TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView2" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/label2" android:layout_alignParentLeft="true"></TextView>
<TextView android:text="TextView" android:textColor="#2CD7E0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_below="@+id/label" android:layout_toRightOf="@+id/label3" android:layout_marginLeft="15dp"></TextView>

         </RelativeLayout>

这是我的活动

    public class SampleActivity extends Activity 
     {
    ListView listview;
   CustomAdap adapter;
   ArrayList<SubjectClass> values = new ArrayList<SubjectClass>();


@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.list1);
    SubjectClass subjectClass = new SubjectClass();
    subjectClass.sCode="cs9410";
    subjectClass.sName="Principles of management";
    subjectClass.teacher="xyz";
    values.add(subjectClass);
    SubjectClass subjectClass1 = new SubjectClass();
    subjectClass1.sCode="cs9401";
    subjectClass1.sName="Ethics";
    subjectClass1.teacher="xxx";
    values.add(subjectClass1);

    listview =(ListView) findViewById(R.id.lv1);
    adapter = new CustomAdap(this, values);
    listview.setAdapter(adapter);
}

} //

2 个答案:

答案 0 :(得分:1)

在行布局文件中将android:text="@+id/label1"更改为android:id="@+id/label1"。 对于所有3 TextView

 SubjectClass sClass = (SubjectClass) names.get(position);

答案 1 :(得分:0)

我认为您需要将代码更改为

 SubjectClass sClass= names.get(position);