简体   繁体   English

呼叫需要API级别16(当前最小值为14)

[英]Call requires API level 16 (current min is 14)

I have this line in my code: 我的代码中有这一行:

linearLayout.setBackground(drawable);

setBackground() shows the following error: setBackground()显示以下错误:

Call requires API level 16 (current min is 14) 呼叫需要API级别16(当前最小值为14)

What does this mean? 这是什么意思?

Can I raise my API level? 我可以提高我的API级别吗?

What does a higher/lower API level mean? 更高/更低的API级别意味着什么?

Does it limit the amount of devices my app can be used on if the API level is higher? 如果API级别更高,是否会限制我的应用可以使用的设备数量?

What does this mean? 这是什么意思?

It means that the setBackground(Drawable) method was added in API Level 16, and older devices do not have it. 这意味着在API级别16中添加了setBackground(Drawable)方法,而较旧的设备没有。 However, your app's minSdkVersion is 14. So, unless you take steps to avoid this line on API Level 14 and 15 devices, your app will crash on those devices. 但是,您的应用程序的minSdkVersion为14.因此,除非您采取措施避免在API级别14和15设备上使用此行,否则您的应用程序将在这些设备上崩溃。

Can I raise my API level? 我可以提高我的API级别吗?

You could set your minSdkVersion to 16. 您可以将minSdkVersion设置为16。

Does it limit the amount of devices my app can be used on if the API level is higher? 如果API级别更高,是否会限制我的应用可以使用的设备数量?

Yes. 是。 If you say that your minSdkVersion is 16, then devices running a version of Android older than that cannot run your app. 如果你说你的minSdkVersion是16,那么运行早minSdkVersion版本的Android的设备将无法运行你的应用。

At the time of this writing, about 90% of Android devices accessing the Play Store are on API Level 16 or higher (ie, are running Android 4.1 or higher). 在撰写本文时, 大约90%访问Play商店的Android设备都在API级别16或更高级别 (即运行Android 4.1或更高版本)。

You can read more about the concept of API levels in the documentation . 您可以在文档中阅读有关API级别概念的更多信息。 Note that this documentation is a bit old, in that it focuses on the minSdkVersion being defined in the manifest. 请注意,此文档有点陈旧,因为它侧重于清单中定义的minSdkVersion In Android Studio projects, minSdkVersion is usually defined in your app module's build.gradle file. 在Android Studio项目中, minSdkVersion通常在app模块的build.gradle文件中定义。

Can I raise my API level? 我可以提高我的API级别吗?

You can set API to 16 it will limit device below 16 but it will perfectly work on devices API 16 and higher 您可以将API设置为16,它会将设备限制在16以下,但它将完全适用于API 16及更高版本的设备

Or else You can use API check and set 否则您可以使用API​​检查和设置

final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.ready) );
} else {
    layout.setBackground( getResources().getDrawable(R.drawable.ready));
}

Use a validation method that will be supported only in API LEVEL >= 16 (note the use of ContextCompat class: 使用仅在API LEVEL> = 16中支持的验证方法(注意使用ContextCompat类:

  if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            linearLayout.setBackground(ContextCompat.getDrawable(this, drawableid));
        }

采用:

layout.setBackgroudResource(R.drawable.ready);       

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

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