こんにちは、リストビューを実装しようとしていますが、これはTextViewでクラスの内容を表示します。しかし、アダプターはnullpointer例外をスローするので、オブジェクトの値を取り込まないようです。アンドロイドでカスタムクラスを使用しているときにListviewのNullpointer例外が発生しました
これはbelowsのは、これはこれは私のあるアダプターで使用される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>
である私の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;
}
で考える
public class SubjectClass {
String sName;
String sCode;
String teacher;
}
私のカスタムクラスでありますアクティビティ
に行レイアウトファイル変更android:text="@+id/label1"
で
} //
にコードを変更する必要があります信じています。 – PravinCG