java无法为安卓执行方法:安卓中的onClick
我似乎无法弄清这件事的真相。使用ApachePOI读取excel文件。似乎在出错。使应用程序强制关闭
java.lang.IllegalStateException: Could not execute method for 安卓:onClick
我的XML 安卓:onClick id与我的方法侦听器具有相同的名称:
<Button
安卓:text="OK"
安卓:id="@+id/submitOK"
安卓:textAlignment="center"
安卓:textAppearance="@style/TextAppearance.AppCompat.Caption"
安卓:textSize="12sp"
安卓:background="@drawable/round_button"
安卓:gravity="center_vertical|center_horizontal"
安卓:textColor="#fff"
安卓:layout_alignTop="@+id/setName"
安卓:layout_toRightOf="@+id/setName"
安卓:layout_marginLeft="5dp"
安卓:layout_width="30dp"
安卓:layout_height="30dp"
安卓:layout_alignBottom="@+id/setName"
**安卓:onClick="inRoster"**
安卓:visibility="gone"
/>
public void inRoster(View v) throws IOException {
if (checkValidinFile()) {
// Send to a new page that lists your shifts, what time you want your alarms
// and what time you want messages to be sent
// What time do you want to send the messages?
// What time do you want your alarms to be?
// Do all alarm and message sending bs
} else {
// Display a toast message saying name not found
// Clear contents of the editbox for the name for user to re enter
CharSequence text = "Name not found in Roster!";
setName.setText("");
int duration = Toast.LENGTH_SHORT;
Toast.makeText(this, text, duration).show();
}
}
这似乎是这种方法出现错误的地方,但我不明白为什么
public boolean checkValidinFile() throws IOException {
String nameToMatch = setName.getText().toString().toLowerCase();
try {
InputStream ExcelFileToRead = new FileInputStream(roster);
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> iterator = sheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
System.out.println(currentCell.toString() + " ");
}
}
System.out.println();
wb.close();
ExcelFileToRead.close();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
“SetName”在类的顶部全局定义,没有实例化,然后在onCreate中定义为:
setName = (EditText) findViewById(R.id.setName);
非常感谢您的帮助,请告诉我您还需要哪些其他信息,我还遇到了一些关于I/art拒绝重新初始化以前失败的类的其他问题。如果您认为这两个问题可能有关联,请参阅下面的链接
# 1 楼答案
用Android5Xlsx项目jar文件替换Apache POI修复了我的问题。现在可以读取/写入excel文件而不会出现任何错误。简单易修复。结论:Apache POI是一种疼痛
# 2 楼答案
您根本无法更改方法签名——包括抛出声明。你需要捕捉IOException,这意味着,即使你通过重新刷新来响应(在这种情况下,你会崩溃)
我还假设这个函数在活动类中。如果不是,这也是一个问题