Android:在较旧的API设备上捕获错误/异常

时间:2017-11-24 21:37:17

标签: android

我正在使用需要API 21的AudioTrack.setVolume。我已将minSdkVersion设置为16.低于处理旧设备上捕获错误的正确方法吗?

    try {
        audioTrack.setVolume(volume);
    }catch (Exception e){
        // Ignore - setVolume requires API 21 and minSdkVersion is set to 16
        // Also ignore any IllegalStateExceptions
    }catch(Error e){
        // Ignore
    }

我有AudioTrack.Builder的类似代码块需要API 23.尽管有阻塞,但这会导致Didn't find class "android.media.AudioTrack$Builder错误。

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式检查API的版本,而不是 try-catch

public class SplitPaneLayout extends ViewGroup {
//...
    private void extractAttributes(Context context, AttributeSet attrs) {
            if (attrs != null) {
                TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SplitPaneLayout);
                mOrientation = a.getInt(R.styleable.SplitPaneLayout_orientation, 0);
                //next line gives ERROR!
                mSplitterSize = a.getDimensionPixelSize(R.styleable.SplitPaneLayout_splitterSize, context.getResources().getDimensionPixelSize(R.dimen.spl_default_splitter_size)); //ERROR HERE