2014-12-16 8 views
0

У меня проблема со списком ListView, и я постоянно получаю непредвиденную ошибку :( , когда я пытаюсь запустить его на своем SamsungGalaxy s4 mini, я получаю сообщение об ошибке, и он падает, и через несколько секунд он поворачивается . от того, что я делаю неправильно это мой код:ListView получает непредвиденную ошибку

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

сообщение LogCat .... –

+0

12-16 16: 13: 11,445 16128-16128/app.nikos.apostolakis.studentsresults.studentsresults E/AndroidRuntime: неустранимый: основные android.content.res.Ресурсы $ NotFoundException: Идентификатор ресурса строки # 0x2f7c57 на android.content.res.Resources.getText (Resources.java:1057) на android.widget.TextView.setText (TextView.java:4183) – kaposnick

+0

эта строка: 'holder. JoinedRank.setText (student.JoinedRank); '. прочитайте документ для этого метода. – njzk2

ответ

1

Убедитесь, что все ресурсы существует в их соответствующих XMLs макета.

R.id.FirstName

R.id.LastName

R.id.JoinedYear

R.id.JoinedRank

Р .id.HelloWorld

Редактировать:

Вы установили текст текстового вида из Integer.

Заменить

holder.JoinedRank.setText(student.JoinedRank); 

с этим

holder.JoinedRank.setText(student.JoinedRank+""); 

Good Luck. :)

+0

ОК. i post также мои файлы макета – kaposnick

+0

Если вы находитесь на затмении, вы можете попробовать Project >> Clean ... –

+0

unrelated. logcat не указывает на отсутствующий идентификатор, но на недостающую строку. – njzk2

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); 

и он должен работать.

Смежные вопросы