Parcelable error尝试在空对象引用上调用虚方法

时间:2016-10-20 04:01:17

标签: java android parcelable

我正在尝试使用parcelable制作一个简单的编辑表单。数据是从数组列表中接收的。但是,当我尝试运行它时,会出现错误:

  

尝试在空对象引用上调用虚方法'int com.insiteprojectid.practiceadapter.user.Student.getId()'

这些是我的代码:

public class StudentActivity extends AppCompatActivity {

private ListView lv;
private CustomUsersAdapter customUsersAdapter;
private TextView emptyTextView;
private StaticStudent staticStudent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_student);
    customUsersAdapter = new CustomUsersAdapter(this, new ArrayList<Student>());
    lv = (ListView)findViewById(R.id.listView);
    lv.setAdapter(customUsersAdapter);
    emptyTextView = (TextView)findViewById(R.id.emptyView);
    lv.setEmptyView(emptyTextView);
    staticStudent = StaticStudent.getInstance();

    FloatingActionButton floatingActionButton = (FloatingActionButton)findViewById(R.id.fabButton);
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),AddStudentActivity.class);
            intent.putExtra("action","add");
            startActivity(intent);
        }
    });

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getApplicationContext(), AddStudentActivity.class);
            Student student = staticStudent.get(position);
            intent.putExtra("StudentList", student);
            startActivity(intent);
        }
    });
}

private void populateStudentDummies() {
    ArrayList<Student> studentList = new ArrayList<>();
    studentList.add(new Student(1, "3135136188", "TRI FEBRIANA SIAMI", "tri.febriana@unj.ac.id", "021577888"));
    studentList.add(new Student(2, "3135136192", "UMMU KULTSUM", "ummu.kultsum@unj.ac.id", "021577888"));
    studentList.add(new Student(3, "3135136215", "ANDREAN OKTAVIANUS H.S.", "andrean.ohs@unj.ac.id", "021577888"));
    staticStudent.AddStudents(studentList);
    customUsersAdapter = new CustomUsersAdapter(this,staticStudent.getList());
    lv.setAdapter(customUsersAdapter);
}

@Override
protected void onResume() {
    //overriding method
    super.onResume();
    //check size of student list
    if(staticStudent.count()==0) {
        customUsersAdapter = new CustomUsersAdapter(this, new ArrayList<Student>());
        //set emptyView
        emptyTextView.setText("No Student Found");
    } else{
        customUsersAdapter = new CustomUsersAdapter(this, staticStudent.getList());
    }
    lv.setAdapter(customUsersAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_student_list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.createDummy:
            populateStudentDummies();
            return true;
        case R.id.clearList:
            StaticStudent.getInstance().clearList();
            customUsersAdapter = new CustomUsersAdapter(this, new ArrayList<Student>());
            lv.setAdapter(customUsersAdapter);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Parcelable子类:

public class Student implements Parcelable{
    private int id;
    private String noreg;
    private String name;
    private String mail;
    private String phone;

    public Student(int id, String noreg, String name, String mail, String phone){
        this.id = id;
        this.name = name;
        this.mail = mail;
        this.phone = phone;
        this.noreg = noreg;
    }

    protected Student(Parcel in) {
        this.id = in.readInt();
        this.noreg = in.readString();
        this.name = in.readString();
        this.mail = in.readString();
        this.phone = in.readString();
    }

    public Student(){

    }

    public static final Creator<Student> CREATOR = new Creator<Student>() {
        @Override
        public Student createFromParcel(Parcel in) {
        return new Student(in);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNoreg() {
        return noreg;
    }

    public void setNoreg(String noreg) {
        this.noreg = noreg;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(getId());
        dest.writeString(getNoreg());
        dest.writeString(getName());
        dest.writeString(getMail());
        dest.writeString(getPhone());
    }
}

然后是方法

public class StaticStudent {
    private static ArrayList<Student> studentList = new ArrayList<>();
    private static StaticStudent instance = new StaticStudent();

    public static ArrayList<Student> getStudentList() {
        return studentList;
    }

    public static void setStudentList(ArrayList<Student> studentList) {
        StaticStudent.studentList = studentList;
    }

    public static StaticStudent getInstance() {
        return instance;
    }

    public static void setInstance(StaticStudent instance) {
        StaticStudent.instance = instance;
    }

    public void addStudent(Student student){
        student.setId(nextId());
        studentList.add(student);
    }

    public int nextId(){
        return studentList.size()+1;
    }

    public Student removeLast(){
        Student student = studentList.remove(studentList.size()-1);
        return student;
    }

    public Student get(int index){
        Student student = studentList.get(index);
        return student;
    }

    public void set(int index, Student student){
        studentList.set(index, student);
    }

    public Student remove(int index){
        Student student = studentList.remove(index);
        resetCounterId(index);
        return student;
    }

    public Student getLast(){
        Student student = studentList.get(studentList.size() - 1);
        return student;
    }

    public void AddStudents(ArrayList<Student> students){
        studentList.addAll(students);
        resetCounterId(0);
    }

    public ArrayList<Student> getList(){
        return studentList;
    }

    public int count(){
        return studentList.size();
    }

    private void resetCounterId(int i){
        for (int j = i; j < studentList.size(); j++) {
            studentList.get(j).setId(j);
        }
    }

    public void clearList(){
        studentList.clear();
    }

}    

这是logcat

  

10-20 11:23:34.753 22842-22842 / com.insiteprojectid.practiceadapter E / AndroidRuntime:FATAL EXCEPTION:main   过程:com.insiteprojectid.practiceadapter,PID:22842   java.lang.RuntimeException:无法启动活动ComponentInfo {com.insiteprojectid.practiceadapter / com.insiteprojectid.practiceadapter.AddStudentActivity}:java.lang.NullPointerException:尝试调用虚拟方法'int com.insiteprojectid.practiceadapter.user.Student。对空引用引用的getId()'   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)   在android.app.ActivityThread.access $ 800(ActivityThread.java:169)   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1421)   在android.os.Handler.dispatchMessage(Handler.java:111)   在android.os.Looper.loop(Looper.java:194)   在android.app.ActivityThread.main(ActivityThread.java:5546)   at java.lang.reflect.Method.invoke(Native Method)   在java.lang.reflect.Method.invoke(Method.java:372)   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:967)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)   引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int com.insiteprojectid.practiceadapter.user.Student.getId()'   at com.insiteprojectid.practiceadapter.AddStudentActivity.onCreate(AddStudentActivity.java:30)   在android.app.Activity.performCreate(Activity.java:5975)   在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)   在android.app.ActivityThread.access $ 800(ActivityThread.java:169)   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1421)   在android.os.Handler.dispatchMessage(Handler.java:111)   在android.os.Looper.loop(Looper.java:194)   在android.app.ActivityThread.main(ActivityThread.java:5546)   at java.lang.reflect.Method.invoke(Native Method)   在java.lang.reflect.Method.invoke(Method.java:372)   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:967)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

0 个答案:

没有答案