我正在编写一个Android应用程序而且我遇到了问题。 该应用程序使用双重连接列表。活动创建一个记录,并在单击按钮时保存调用一个方法,该方法在列表的add方法中传递创建的记录。 它只是第一次工作,然后抛出一个NullPointerException,调用List Node的构造函数。 错误只发生在android中,在PC上运行良好。 这是代码和抛出的异常。
public boolean add(Object data) {
boolean control = false;
try {
if (isEmpty()) {
ListNode newNode = new ListNode(data);
setFirstNode(newNode);
setCurrentNode(newNode);
incrementNumberOfNodes();
control = true;
}
else {
ListNode newNode = new ListNode(data, currentNode.getNextNode(), currentNode);
if (currentNode.getNextNode() != null) {
currentNode.getNextNode().setPreviousNode(newNode);
}
currentNode.setNextNode(newNode);
currentNode = newNode;
incrementNumberOfNodes();
control = true;
}
} catch (NullPointerException e) {
System.err.println(e);
}
return control;
}
public void save(View v){
String action = getIntent().getAction();
if(action.compareTo(GlobalVariables.ACTION_NEW) == 0) {
newRecord();
System.out.println("Save finish with new Record");
}
}
private void newRecord(){
EditText title = (EditText) findViewById(R.id.title);
EditText password = (EditText) findViewById(R.id.password);
int key = GlobalVariables.firstFreePosition();
Record newRecord = new Record(key,title.getText().toString());
newRecord.setPassword(password.getText().toString());
System.out.printf("RECORD CREATO: %s\n", newRecord);
// Aggiungo il record alla lista
boolean control = GlobalVariables.recordsList.add(newRecord);
System.out.println("New Record = " + control);
}
这是构造函数:
protected ListNode(Object data, ListNode nextNode, ListNode previousNode){
setData(data);
setNextNode(nextNode);
setPreviousNode(previousNode);
}
这是例外(list.java'第120行是列表节点构造的调用):
09-09 21:27:39.711 19605-19605/cf.portaChiavi E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
Caused by: java.lang.NullPointerException
at cf.list.List.add(List.java:120)
at cf.portaChiavi.NewRecord.newRecord(NewRecord.java:161)
at cf.portaChiavi.NewRecord.save(NewRecord.java:90)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
请帮帮我。 感谢。