NullPointerException学生类组织者android

时间:2014-03-03 05:49:09

标签: java android listview nullpointerexception

所以我正在为学生做一个简单的班级经理,我无法弄清楚这一点。在主活动中有一个添加类的按钮,它会将您带到添加类活动,该活动具有添加学生按钮,可以将您带到添加学生活动。当我保存学生时,通过返回添加课程活动并将学生姓名添加到名单列表视图,它可以正常工作。但由于某种原因,当我保存类时,我在返回主活动时得到nullPointerException。如果有人有任何想法,我真的很感激帮助。

以下是主要活动:

public class MainActivity extends Activity {

ListView classList;
Button addClass;
ArrayAdapter<String> adapterClass;
ArrayList<Class> currClasses = new ArrayList<Class>();
ArrayList<String> currClassNames = new ArrayList<String>();
ClassesSingleton myClasses;
int REQUEST_CODE_ADD = 318;
int REQUEST_CODE_EDIT = 319;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    classList = (ListView) findViewById(R.id.classList);
    final Context context = this;
    adapterClass = new ArrayAdapter<String>(this, R.layout.single_list_item, R.id.label, currClassNames);

    classList.setAdapter(adapterClass);

    addClass = (Button) this.findViewById(R.id.addClass);

    classList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView <?> arg0, View view, int position, long id){
            Intent intent = new Intent(MainActivity.this, ShowClass.class);
            startActivityForResult(intent, REQUEST_CODE_EDIT);
        }
    });

    addClass.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0){
            Intent intent = new Intent(MainActivity.this, AddClass.class);
            startActivityForResult(intent, REQUEST_CODE_ADD);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK && requestCode == REQUEST_CODE_ADD){
        Class newClass = new Class();
        if(data.hasExtra("className")){
            newClass.setName(data.getExtras().getString("name"));
        }
        if(data.hasExtra("number")){
            newClass.setNumber(data.getExtras().getInt("number"));
        }
        if(data.hasExtra("students")){
            ArrayList<Student> s = data.getExtras().getParcelableArrayList("students");
            newClass.setStudents(s);
        }
        if(data.hasExtra("names")){
            ArrayList<String> sn = data.getExtras().getStringArrayList("names");
            newClass.setStudentNames(sn);
        }

        myClasses = ClassesSingleton.getInstance();
        updateClassList(newClass, newClass.getName());
        myClasses.setClassArray(currClasses);
        myClasses.setClassNames(currClassNames);
    }

    //else if(resultCode == RESULT_OK && requestCode == REQUEST_CODE_EDIT){

    //}
}

public void updateClassList(Class n, String s){

    adapterClass = null;
    currClasses.add(n);
    currClassNames.add(s);
    adapterClass = new ArrayAdapter<String>(this, R.layout.single_list_item, R.id.label, currClassNames);
    classList.setAdapter(adapterClass);
    adapterClass.notifyDataSetChanged();
}
}

这是add class活动:

public class AddClass extends Activity{

Button addStudent, saveClass, cancelClass;
EditText className, classNumber;
ListView studentList;
ArrayAdapter<String> adapterStudent;
ArrayList<Student> currStudents = new ArrayList<Student>();
ArrayList<String> studNames = new ArrayList<String>();
int REQUEST_CODE_ADDSTU = 317;
int REQUEST_CODE_EDSTU = 316;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    setContentView(R.layout.new_class);

    addStudent = (Button) findViewById(R.id.addStudent);
    saveClass = (Button) findViewById(R.id.confirmClassAdd);
    cancelClass = (Button) findViewById(R.id.cancelClass);
    className = (EditText) findViewById(R.id.newClassName);
    classNumber = (EditText) findViewById(R.id.newClassNumber);
    studentList = (ListView) findViewById(R.id.newStudentList);
    adapterStudent = new ArrayAdapter<String>(this, R.layout.single_list_item, R.id.label, studNames);

    studentList.setAdapter(adapterStudent);

    studentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView <?> arg0, View view, int position, long id){
            Intent intent2 = new Intent(AddClass.this, EditStudent.class);
            startActivityForResult(intent2, REQUEST_CODE_EDSTU);
        }
    });

    addStudent.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0){
            Intent intent2 = new Intent(AddClass.this, AddStudent.class);
            startActivityForResult(intent2, REQUEST_CODE_ADDSTU);
        }
    });

    saveClass.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0){
            Intent data = new Intent();
            int numData = 0;
            if(className.getText().toString().matches("") != true && classNumber.getText().toString().matches("") != true && currStudents.isEmpty() != true && studNames.isEmpty() != true){
                String nameData = className.getText().toString();
                String numStr = classNumber.getText().toString();
                ArrayList<Student> students = currStudents;
                ArrayList<String> studentNames = studNames;

                boolean intTrue = true;
                try{
                    numData = Integer.parseInt(numStr);
                }catch(NumberFormatException e){
                    Toast.makeText(AddClass.this, "Please be sure to enter a numberic value in the number field.", Toast.LENGTH_LONG).show();
                    intTrue = false;
                }

                if(intTrue){
                    data.putExtra("className", nameData);
                    data.putExtra("number", numData);
                    data.putStringArrayListExtra("names", studentNames);
                    data.putParcelableArrayListExtra("students", students);
                    setResult(RESULT_OK, data);
                    finish();
                }
            }
            else{
                Toast.makeText(AddClass.this, "Don't leave your class name, number field, or student roster blank!", Toast.LENGTH_LONG).show();
            }
        }
    });
}

//onActivityResult callback
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK && requestCode == REQUEST_CODE_ADDSTU){
        Student newStudent = new Student();
        if(data.hasExtra("name")){
            newStudent.setName(data.getExtras().getString("name"));
        }
        if(data.hasExtra("id")){
            newStudent.setId(data.getExtras().getInt("id"));
        }

        updateStudentList(newStudent, newStudent.getName());
    }
}

public void updateStudentList(Student s, String n){
    adapterStudent = null;
    currStudents.add(s);
    studNames.add(n);
    adapterStudent = new ArrayAdapter<String>(this, R.layout.single_list_item, R.id.label, studNames);
    studentList.setAdapter(adapterStudent);
    adapterStudent.notifyDataSetChanged();
}

}

以下是添加学生活动:

public class AddStudent extends Activity{

EditText studentName, studentId;
Button saveStudent, cancelStudent;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_student);

    studentName = (EditText) findViewById(R.id.newStudentName);
    studentId = (EditText) findViewById(R.id.newStudentId);
    saveStudent = (Button) findViewById(R.id.confirmStudentAdd);
    cancelStudent = (Button) findViewById(R.id.cancelStudent);

    saveStudent.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0){
            Intent data = new Intent();
            int idData = 0;

            if(studentName.getText().toString().matches("") != true && studentId.getText().toString().matches("") != true){
                String nameData = studentName.getText().toString();
                String idStr = studentId.getText().toString();

                boolean intTrue = true;
                try{
                    idData = Integer.parseInt(idStr);
                }catch(NumberFormatException e){
                    Toast.makeText(AddStudent.this, "Please be sure to enter a numeric value in the ID field.", Toast.LENGTH_LONG).show();
                    intTrue = false;
                }

                if(intTrue){
                    data.putExtra("name", nameData);
                    data.putExtra("id", idData);
                    setResult(RESULT_OK, data);
                    finish();
                }
            }
            else{
                Toast.makeText(AddStudent.this, "Don't leave your student name or ID field blank!", Toast.LENGTH_LONG).show();
            }
        }
    });
}
}

这是堆栈跟踪:

 FATAL EXCEPTION: main
Process: com.example.congelassign2, PID: 1664
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2263)
at android.widget.ListView.makeAndAddView(ListView.java:1790)
at android.widget.ListView.fillDown(ListView.java:691)
at android.widget.ListView.fillFromTop(ListView.java:752)
at android.widget.ListView.layoutChildren(ListView.java:1616)
at android.widget.AbsListView.onLayout(AbsListView.java:2091)
at android.view.View.layout(View.java:14785)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14785)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14785)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
at android.view.View.layout(View.java:14785)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14785)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1985)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1742)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

查看异常似乎'ListView classList'在用于onCreate方法之前没有初始化。

答案 1 :(得分:0)

显示错误,因为您试图在ArrayAdapter课程中显示自定义布局。要在ArrayAdapter中使用自定义布局,您需要创建单独的自定义适配器类,该类扩展ArrayAdapter类并将布局扩展到该适配器类。

你试图夸大布局的方式是错误的,这就是为什么你得到错误at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)因为它将引用android布局将数据绑定到listview。

此外,您的ArrayList ArrayList<String> currClassNames = new ArrayList<String>();ArrayList<String> studNames = new ArrayList<String>();都为空。