我创建了两个类。其中一个参考了另一个:
public class SCHOOL {
private String schoolid;
private String schoolname;
private List<STUDENT> students;
}
//getters and setters for schoolid and schoolname;
public void setStudents(List<STUDENT> students) {
this.students = students;
}
public List<STUDENT> getStudents() {
return this.students;
}
public void addStudent(String names, String age) {
STUDENT student = new STUDENT();
student.setNames(names);
student.setAge(age);
students.add(student);
}
public class STUDENT {
private String names;
private String age;
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
以下是我的主要课程:
public class MainActivity extends AppCompatActivity {
static final String tag = "Tag Activity";
List<STUDENT> studentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
SCHOOL school = new SCHOOL();
school.setStudents(studentList);
List<STUDENT> students = school.getStudents();
school.addStudent("kent", "43");
school.addStudent("Winnie", "42");
school.addStudent("Dennis", "41");
studentList.add((STUDENT) students);
Log.d(tag, String.valueOf(studentList));
.......
以下是我从逻辑上的错误日志:
11-01 21:25:24.348 3049-3049 / com.example.spidey.jsontosql W / dalvikvm:threadid = 1:线程以未捕获的异常退出(group = 0xa4cb4b20)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:FATAL EXCEPTION:main
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:进程:com.example.spidey.jsontosql,PID:3049
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.spidey.jsontosql / com.example.spidey.jsontosql .MainActivity}:java.lang.NullPointerException
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:at android.app.ActivityThread.access $ 800(ActivityThread.java:135)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:102)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:at android.os.Looper.loop(Looper.java:136)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:位于android.app.ActivityThread.main(ActivityThread.java:5001)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(本地方法)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:515)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在dalvik.system.NativeStart.main(本机方法)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:由以下原因引起:java.lang.NullPointerException
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:at com.example.spidey.jsontosql.SCHOOL.addStudent(SCHOOL.java:50)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在com.example.spidey.jsontosql.MainActivity.onCreate(MainActivity.java:35)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在android.app.Activity.performCreate(Activity.java:5231)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:at android.app.ActivityThread.access $ 800(ActivityThread.java:135)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:102)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:at android.os.Looper.loop(Looper.java:136)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:位于android.app.ActivityThread.main(ActivityThread.java:5001)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(本地方法)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:515)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-01 21:25:24.360 3049-3049 / com.example.spidey.jsontosql E / AndroidRuntime:在dalvik.system.NativeStart.main(本机方法)
11-01 21:30:25.020 3049-3049 / com.example.spidey.jsontosql I / Process:正在发送信号。 PID:3049 SIG:9
我尝试使用我的addStudent方法输入数据,但是程序崩溃了。我的代码有问题吗?请帮助我纠正。
最佳答案
我认为您的错误是:
studentList.add((STUDENT) students);
您不能直接将列表投给学生。循环浏览列表中的每个学生并添加他们。
for (Student student: studentList) {
studentList.add(student);
}
下次提供错误日志
关于java - 我的Android类(class)中的Arraylist错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33469769/