Java 调用需要 API 级别 11(当前最小值为 8)android.app.Activity#onCreateView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32181705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:07:32  来源:igfitidea点击:

Call requires API level 11(current min is 8) android.app.Activity#onCreateView

javaandroid

提问by Manoj Majumdar

I am a newbie to android, and I am developing an android application. But my package line gives this error in MainActivity.java class. Could anyone please tell me the reason of this?. This is my class and the package line gives this error.

我是 android 的新手,我正在开发一个 android 应用程序。但是我的包行在 MainActivity.java 类中给出了这个错误。谁能告诉我这是什么原因?这是我的课程,包行给出了这个错误。

package com.example.eventgyaam;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity  {

int counter;

Button add,sub;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter = 0;
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (Button) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter++;
            display.setText("Your total is "+counter);
        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter--;
            display.setText("Your total is "+counter);
        }
    });

}
}

采纳答案by paxdiablo

First, some introductory details. There are levelsof Android API and, at each level, they may introduce new functionality , as per here.

首先,一些介绍性细节。Android API有多个级别,在每个级别,它们可能会引入新功能,如此处所示

When you create an Android application, you specify the minimum level that your application will run on. This is done in the manifestwith the uses-sdkstanza: see herefor details.

创建 Android 应用程序时,您需要指定应用程序运行的最低级别。这是在做manifestuses-sdk节:看到这里了解详情。

So, if you use functionality that's only available at level 11, your code won'trun on a level 8 API like you've specified.

因此,如果您使用仅在 11 级可用的功能,您的代码将不会像您指定的那样在 8 级 API 上运行。

You either have to use only what's available at your current minimum, or up the minimum.

您要么只使用当前最低限度可用的资源,要么使用最低限度。

As to why you appear to be getting the error on the package line, and for a call that doesn't seem to be in the source code you've shown us, I couldn't say. Since it's complaining about android.app.Activity#onCreateViewas per your title, there maybe an issue with the view R.layout.activity_mainthat's being inflated by setContentView. Or it may be an issue with the compatibility library itself, something that's not unheard of.

至于为什么您似乎在包行上收到错误,以及对于您向我们展示的源代码中似乎没有的调用,我不能说。由于它在抱怨android.app.Activity#onCreateView按你的标题,有可能是这样的观点的问题R.layout.activity_main多数民众赞成被夸大setContentView。或者它可能是兼容性库本身的问题,这并非闻所未闻

However, rather than struggling toohard with this, you may want to consider simply upping the minimum to API-11 and dropping the appcompatstuff altogether. As per here, only about 2% of Android users are still back on releases earlier than Android-3.0/API-11 (as of June 2016). That's only likely to get smaller as time goes by.

然而,而不是挣扎这种努力,你可能要考虑只是加大了最低至API-11和丢弃appcompat完全的东西。根据这里,只有大约 2% 的 Android 用户仍然使用早于 Android-3.0/API-11(截至 2016 年 6 月)的版本。随着时间的推移,这只可能会变得更小。

回答by Ravi

change your minsdkversion from manifest.xml

从 manifest.xml 更改您的 minsdkversion

<uses-sdk
    android:minSdkVersion="11"/>

回答by Hussein El Feky

For each Android API, there might be new features added, which can't work on lower API versions. Thus, to fix this, you can just increase the minimum required API in your project.

对于每个 Android API,可能会添加新功能,这些功能无法在较低的 API 版本上运行。因此,要解决此问题,您只需增加项目中所需的最低 API。

In your build.gradle (app-module), you will find this line:

在您的 中build.gradle (app-module),您会发现这一行:

minSdkVersion 8

Change it to:

将其更改为:

minSdkVersion 11

If you didn't find this line in build.gradle, check your AndroidManifest.xmland change the minSdkVersionfrom 8 to 11 or whatever you want:

如果您没有在 中找到这一行build.gradle,请检查您的AndroidManifest.xml并将minSdkVersion8更改为 11 或任何您想要的:

<uses-sdk
    android:minSdkVersion="11" />

But now your app will only work on API 11+, which is fine. Nobody uses below API 11 these days, so it shouldn't be a problem for you.

但是现在您的应用程序只能在 API 11+ 上运行,这很好。这些天没有人使用低于 API 11 的内容,因此这对您来说应该不是问题。

回答by Rajesh

Error:

错误:

Call requires API level 11(current min is 8) android.app.Activity#onCreateView

调用需要 API 级别 11(当前最小值为 8)android.app.Activity#onCreateView

If you take a look at the javadoc for the Activity class you'll see that the method public View onCreateView (View parent, String name, Context context, AttributeSet attrs)was added in API 11 that is why you are getting this error.

如果您查看 Activity 类的 javadoc,您会看到该方法public View onCreateView (View parent, String name, Context context, AttributeSet attrs)已添加到 API 11 中,这就是您收到此错误的原因。

Either You have to Change your minSdkVersionfrom 8to 11

要么你必须改变你的minSdkVersion811

Or you Use @SuppressLint("NewApi")at the class declaration level as workaround for Activitylike this:

或者您@SuppressLint("NewApi")在类声明级别使用作为解决方法Activity

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {

For Fragment:

对于Fragment

@SuppressLint("NewApi")
@Override
public void View onCreateView(String name, Context context, AttributeSet attrs) {

@SuppressLint("NewApi")is an annotation used by the Android Lint tool.

@SuppressLint("NewApi")是 Android Lint 工具使用的注释。

Lint will tell you whenever something in your code isn't optimal or may crash. By passing NewApi there, you're suppressing all warnings that would tell you if you're using any API introduced after your minSdkVersion

每当您的代码中的某些内容不是最佳的或可能崩溃时,Lint 都会告诉您。通过在那里传递 NewApi,您将抑制所有警告,这些警告会告诉您是否正在使用在您之后引入的任何 APIminSdkVersion

EDIT:

编辑:

You can also use @TargetApi(11)instead of @SuppressLint("NewApi"). More difference between these can be found here.

您也可以使用@TargetApi(11)代替@SuppressLint("NewApi")。可以在此处找到这些之间的更多区别。

Hope it helps!

希望能帮助到你!

回答by shayyy7

I faced the same issue and I resolved it by changing the superclass of my activity. Insted of "public class MainActivity extends AppCompatActivity", I typed "public class MainActivity extends Activity". After the change the issue got resolved.

我遇到了同样的问题,我通过更改活动的超类来解决它。插入“公共类 MainActivity 扩展 AppCompatActivity”,我输入了“公共类 MainActivity 扩展了 Activity”。更改后问题得到解决。

回答by M D P

If you are using Eclipse, right click on one of your projects and click properties, select Java Compiler from left list, uncheck "Enable project specific settings", click on Configure Workspace Settings link, change Compiler compliance level to the highest version (for now it's 1.8). then click OK and accept everything it asked.

如果您使用的是 Eclipse,请右键单击您的项目之一并单击属性,从左侧列表中选择 Java 编译器,取消选中“启用项目特定设置”,单击配置工作区设置链接,将编译器合规性级别更改为最高版本(目前它是 1.8)。然后单击“确定”并接受它要求的所有内容。

Also to make sure, change Project Build Target api of all of your projects to the latest api.

另外为了确保,将所有项目的 Project Build Target api 更改为最新的 api。

EDIT: I personally had to uncheck "Enable project specific settings" for all of library projects one by one. changed Compiler compliance level of workspace to 1.8 then checked "Enable project specific settings" only for my own main project and changed its Compiler compliance level to 1.6.

编辑:我个人不得不为所有库项目一一取消选中“启用项目特定设置”。将工作区的编译器合规级别更改为 1.8,然后仅为我自己的主项目选中“启用项目特定设置”,并将其编译器合规级别更改为 1.6。

回答by Error

Because fragments added in API 11 and your current minSDK = 8, you can see that here

因为在 API 11 中添加了片段并且您当前的 minSDK = 8,您可以在这里看到

Your are using AppCompatActivity which calls Fragment class in onCreateView() method, Although your code doesn't implement onCreateView() but AppCompatActivity does. you can read the source code here

您使用的 AppCompatActivity 在 onCreateView() 方法中调用 Fragment 类,虽然您的代码没有实现 onCreateView() 但 AppCompatActivity 实现。你可以在这里阅读源代码

Solutionthere are two possible solution

解决方案有两种可能的解决方案

1- change sdk version in manifest to >= 11

1- 将清单中的 sdk 版本更改为 >= 11

2- (if you use API below 11) use SuppressLint annotation to suppress any error related to sdkVersion and implement onCreateView as follows

2-(如果您使用低于 11 的 API)使用 SuppressLint 注释来抑制与 sdkVersion 相关的任何错误并按如下方式实现 onCreateView

@SuppressLint("NewApi")
public View onCreateView(View parent, String name, Context context, AttributeSet attrs)
{
    if (Build.VERSION.SDK_INT >= 11) 
        return super.onCreateView(parent, name, context, attrs);
    return null;
}