简体   繁体   中英

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

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.

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);
        }
    });

}
}

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.

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

minSdkVersion 8

Change it to:

minSdkVersion 11

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

<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.

Error:

Call requires API level 11(current min is 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.

Either You have to Change your minSdkVersion from 8 to 11

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

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

For 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.

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

EDIT:

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

Hope it helps!

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

When you create an Android application, you specify the minimum level that your application will run on. This is done in the manifest with the uses-sdk stanza: see here for details.

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

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#onCreateView as per your title, there may be an issue with the view R.layout.activity_main that's being inflated by setContentView . Or it may be an issue with the compatibility library itself, something that's not unheard of .

However, rather than struggling too hard with this, you may want to consider simply upping the minimum to API-11 and dropping the appcompat stuff 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.

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

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

Solution there are two possible solution

1- change sdk version in manifest to >= 11

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

@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;
}

change your minsdkversion from manifest.xml

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

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.

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.

Also to make sure, change Project Build Target api of all of your projects to the latest 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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