【问题标题】:Could not execute method for android:onClick in Android无法在 Android 中执行 android:onClick 的方法
【发布时间】:2017-06-26 12:58:56
【问题描述】:

我似乎无法深入了解这一点。使用 Apache POI 读取 excel 文件。似乎遇到了错误。使应用强制关闭。

java.lang.IllegalStateException: Could not execute method for android:onClick

Full error output

我的 XML android:onClick id 与我的方法侦听器同名:

 <Button
    android:text="OK"
    android:id="@+id/submitOK"
    android:textAlignment="center"
    android:textAppearance="@style/TextAppearance.AppCompat.Caption"
    android:textSize="12sp"
    android:background="@drawable/round_button"
    android:gravity="center_vertical|center_horizontal"
    android:textColor="#fff"
    android:layout_alignTop="@+id/setName"
    android:layout_toRightOf="@+id/setName"
    android:layout_marginLeft="5dp"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignBottom="@+id/setName"
    **android:onClick="inRoster"**
    android: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 拒绝重新初始化以前失败的课程。如果您认为这两个问题可能相关,请参阅下面的链接。

I/art rejection

【问题讨论】:

  • 我认为它被否决的唯一原因是因为它是如此“容易解决”,如果是这样,为什么你不投反对票并离开页面,而不是投反对票,回答问题然后离开。我查看了其他类似的答案,检查了类似实例的代码并尝试了这些修复,但没有解决任何问题。

标签: java android apache-poi


【解决方案1】:

用 Android5Xlsx 项目 jar 文件替换 Apache POI 解决了我的问题。现在可以读取/写入 excel 文件而不会出现任何错误。简单易修复。结论:Apache POI 很麻烦。

【讨论】:

    【解决方案2】:

    您根本无法更改方法签名 - 包括 throws 声明。您需要捕获该 IOException,这意味着即使您通过重新抛出响应(在这种情况下您将崩溃)。

    我还假设该函数在您的活动类中。如果不是,那也是个问题。

    【讨论】:

    • 尝试删除 throws 并添加 try catch,仍然出现相同的错误。没有运气
    • 还有其他想法吗?拉弦
    • 是的,我认为我误读了您的初始问题。发布完整的堆栈跟踪,我认为您错过了真正的错误(它总是最底部的错误)。
    • 您真正的问题是由以下原因引起的:java.lang.ClassNotFoundException:在路径上找不到类“javax.xml.stream.XMLEventFactory”:DexPathList [[dex 文件
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    相关资源
    最近更新 更多