12

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

}
}
Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
Manoj Majumdar
  • 505
  • 1
  • 4
  • 23
  • Can you post your Activity implementation as well as the build.gradle file for your module? – vman Sep 01 '15 at 01:28
  • 1
    This is an Android SDK bug. I recommend starring it [here](https://code.google.com/p/android/issues/detail?id=188677) to help the developers know how many people are affected. – nkorth Oct 07 '15 at 17:29

7 Answers7

6

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.

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
  • 1
    Well, 5% are still using pre-3.0 but that's close enough to zero (and likely to get smaller as people upgrade their phones) in terms of a marketing decision :-) – paxdiablo Sep 01 '15 at 01:24
  • @paxdiablo in my country, people using pre-3.0 are about 30% of the Android users (API level 10) – EpicPandaForce Oct 07 '15 at 18:15
  • Epic, that may be so but mobile developers rarely target a single country, they're more interested in the worldwide market, especially at $0.99 a pop :-) – paxdiablo Oct 07 '15 at 22:58
5

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 9
    This answer is wrong, it seems you haven't read the question. In his code there is no functionality that he uses from api level 11. After sdk update I am getting same error, it seems there is a bug with their new sdk. – user65721 Aug 30 '15 at 08:12
  • @user65721, there is also no oncreateview in the code either, which is what the error message is complaining about. I made no statement about the code given, only about how the API levels worked. If you can point to a *specific* statement of mine which is wrong (rather than a mis-reading of the answer), I'd be happy to fix it. – paxdiablo Aug 30 '15 at 23:23
  • 3
    @ paxdiablo sorry for my poor wording, what I meant is, your post does not answer the question, your statements are correct and also helpful however it is not the correct answer for this question. – user65721 Aug 31 '15 at 09:58
  • 1
    @user65721 that isn't true you can see my answer below. – Error Oct 21 '16 at 08:41
  • For those who care, https://stackoverflow.com/questions/3423754/retrieving-android-api-version-programmatically provides a way of writing legacy code for older APIs where necessary, while still providing the shiny new features. – Kraigolas Sep 27 '20 at 17:30
5

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!

Community
  • 1
  • 1
Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • 1
    I am brand new with android .... but as a general rule 'error suppression' is not a good idea, especially during development – dsdsdsdsd Apr 25 '16 at 15:41
  • 1
    also ... +1 for the explanation about the `Activity class` ... ... but question: doesn't that effectively mean that the `onCreate` method (that apparently extends `onCreateView` ) is unusable for API < 11 ?? ... thanks. – dsdsdsdsd Apr 25 '16 at 15:45
  • @dsdsdsdsd i have added onCreateView for Fragment because there is no onCreate method in Fragment – Rajesh Jadav Apr 27 '16 at 03:58
1

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;
}
Error
  • 820
  • 1
  • 11
  • 34
0

change your minsdkversion from manifest.xml

<uses-sdk
    android:minSdkVersion="11"/>
Ravi
  • 34,851
  • 21
  • 122
  • 183
0

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.

shayyy7
  • 93
  • 7
  • Is this the full answer? – Enamul Hassan Oct 27 '15 at 09:12
  • This is getting away from the problem. From extending Activity we have moved on to AppCompatActivity for backward Compatibility of newer features and you are suggesting to leave them altogether and work with the same old Activity calss – user2779311 Jan 10 '16 at 03:37
0

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.

M D P
  • 4,525
  • 2
  • 23
  • 35