简体   繁体   English

无法在OnClickListener上抛出IOException

[英]Can't throw IOException on OnClickListener

I'm trying to make an in-app audio recorder, but I am for some reason unable to throw IOException because of a clash with the OnClick method. 我正在尝试制作应用内录音机,但由于与OnClick方法发生冲突,我出于某种原因无法抛出IOException。

play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) throws IOException {

                MediaPlayer m = new MediaPlayer();
                m.setDataSource(outputFile);
                m.prepare();
                m.start();
                Toast.makeText(getContext(), "Playing audio", Toast.LENGTH_SHORT).show();
        }
    });

The throws IOException part is necessary for lines that you can see later, but it seems to cause some sort of conflict. 抛出IOException部分对于稍后可以看到的行是必需的,但它似乎会引起某种冲突。 This whole thing is within the onCreateView method and it is within a fragment, if that is relevant information. 整个事情都在onCreateView方法中,如果是相关信息,它就在片段内。 Any help greatly appreciated! 任何帮助非常感谢!

Method overriding 方法覆盖

Rule: An overriding method (the method of child class) can throw any unchecked exceptions, regardless of whether the overridden method (method of base class) throws exceptions or not. 规则:重写方法(子类的方法)可以抛出任何未经检查的异常,无论被覆盖的方法(基类的方法)是否抛出异常。 However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. 但是,重写方法不应抛出新的或更宽的已检查异常,而不是重写方法声明的异常。 The overriding method can throw those checked exceptions, which have less scope than the exception(s) declared in the overridden method. 重写方法可以抛出那些已检查的异常,这些异常的范围小于重写方法中声明的异常。

In your case onClick() is an overrided method from View.OnClickListener . 在您的情况下, onClick()是来自View.OnClickListener的覆盖方法。 Since onClick() in View.OnClickListener does not throw any exception in parent class/interface so you can not throw a checked Exception . 由于View.OnClickListener中的onClick()不会在父类/接口中抛出任何异常,因此您无法抛出已检查的异常。

IOException is an Checked Exception. IOException是Checked Exception。 You can try to throw ArrayIndexOutOfBoundsException it will not give give compile time error . 你可以尝试抛出ArrayIndexOutOfBoundsException它不会给出编译时错误。

Apart from that throws from onClick() does not seem any use . 除了onClick() throws似乎没有任何用处。 throws used to pass the Exception in calling chain. throws用于在调用链中传递Exception。 Since onClick() is a event listener there is no use of throwing the Exception. 由于onClick()是一个事件监听器,因此不会抛出异常。 You should use try/catch and handle Exception inside onClick() itself . 你应该使用try/catch并在onClick()内部处理Exception。

According to the API, the View.OnClickListener interface defines a method onClick(View view) that doesn't throw any checked exception. 根据API, View.OnClickListener接口定义了一个方法onClick(View view) ,它不会抛出任何已检查的异常。

Therefore, you CANNOT throw an exception. 因此,您不能抛出异常。 Period. 期。 The best option is to catch this exception and do something with it. 最好的选择是捕获此异常并使用它执行某些操作。 Maybe mark the case with a flag, or even store the exception. 也许用标志标记案例,甚至存储异常。

Alternatively, though not recommended, you could throw an unchecked exception. 或者,虽然不推荐,但您可以抛出未经检查的异常。 My guess is, though, the API won't be happy about it. 我的猜测是,API不会对此感到高兴。 Better go with the first option. 最好选择第一个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM