我有一个ListView的问题,我不断得到一个意外的错误:( 当我试图在我的SamsungGalaxy s4 mini上运行它时我得到一个错误并且它崩溃了几秒后它就会关闭。我究竟做错了什么? 这是我的代码:
public class FirstActivity extends Activity {
private ListView listview01;
private TextView TextView01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Student students_data[] = new Student[]
{
new Student("Nikos", "Apostolakis", "2012", "03112023"),
new Student("Stavros", "Birbilis", "2012", "03112116")
};
StudentAdapter adapter = new StudentAdapter(this, R.layout.studentitemlayout, students_data);
listview01 = (ListView) findViewById(R.id.ListView01);
TextView01 = (TextView) findViewById(R.id.HelloWorld);
TextView01.setText("BreakPointReached!");
listview01.setAdapter(adapter);
}
}
我的班级是
public class Student {
public String FirstName;
public String LastName;
public String JoinedYear;
public int JoinedRank;
public Student() {
super();
}
public Student(String FirstName , String LastName , String JoinedYear, String JoinedRank){
this.FirstName = FirstName;
this.LastName = LastName;
this.JoinedYear = JoinedYear;
this.JoinedRank = Integer.parseInt(JoinedRank);
}
}
和
public class StudentAdapter extends ArrayAdapter {
Context context;
int layoutResourceId;
Student data[] = null;
public StudentAdapter(Context context, int layoutResourceId, Student[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
StudentHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new StudentHolder();
holder.FirstName = (TextView) row.findViewById(R.id.FirstName);
holder.LastName = (TextView) row.findViewById(R.id.LastName);
holder.JoinedYear = (TextView) row.findViewById(R.id.JoinedYear);
holder.JoinedRank = (TextView) row.findViewById(R.id.JoinedRank);
row.setTag(holder);
} else {
holder = (StudentHolder) row.getTag();
}
Student student = data[position];
holder.FirstName.setText(student.FirstName);
holder.LastName.setText(student.LastName);
holder.JoinedYear.setText(student.JoinedYear);
holder.JoinedRank.setText(student.JoinedRank);
return row;
}
static class StudentHolder {
TextView FirstName;
TextView LastName;
TextView JoinedYear;
TextView JoinedRank;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
android:id="@+id/FirstName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000" />
<TextView
android:id="@+id/LastName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/JoinedYear"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/JoinedRank"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout 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:orientation="vertical"
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=".FirstActivity">
<TextView
android:id="@+id/HelloWorld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HelloWorld"/>
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
答案 0 :(得分:1)
确保所有资源都存在于相应的布局XML中。
<强> R.id.FirstName 强>
<强> R.id.LastName 强>
<强> R.id.JoinedYear 强>
<强> R.id.JoinedRank 强>
<强> R.id.HelloWorld 强>
编辑:
您已从整数设置文本视图的文本。
替换
holder.JoinedRank.setText(student.JoinedRank);
用这个
holder.JoinedRank.setText(student.JoinedRank+"");
祝你好运。 :)
答案 1 :(得分:0)
Student student = data[position];
holder.FirstName.setText(student.FirstName);
holder.LastName.setText(student.LastName);
holder.JoinedYear.setText(student.JoinedYear);
holder.JoinedRank.setText(student.JoinedRank);
我想你的学生.JoinedRank是一个整数。这会调用setText(int resId)
方法并导致异常,因为R
类中的值不会是2010年的字符串常量等。
将行更改为
holder.JoinedRank.setText("" + student.JoinedRank);
它应该有用。