Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

lint

After the SDK update (23), I am getting this lint error, I haven't made any change in my code and it was working fine on devices with api level 9. Also I do not call android.app.Activity#onCreateView in my code at all. If i click the auto fix, it puts @SuppressLint("NewApi") to the declaration of the class @SuppressLint("NewApi") public class MyActivity extends android.support.v4.app.FragmentActivitylike this and error goes away, I want to be sure if this is the way to go.

like image 795
user65721 Avatar asked Aug 30 '15 08:08

user65721


2 Answers

I encountered the same issue as well.

If you take a look at the javadoc for the Activity class (http://developer.android.com/reference/android/app/Activity.html#onCreateView%28android.view.View,%20java.lang.String,%20android.content.Context,%20android.util.AttributeSet%29), you'll see that the method public View onCreateView (View parent, String name, Context context, AttributeSet attrs) was added in API 11.

Rather than using @SuppressLint("NewApi") at the class declaration level, I added that particular method to my code and suppressed the lint warning for the method declaration. Like so:

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

This way any future additions to the code of the class will still get checked by lint, but lint will stop flagging this method with an error.

ETA: Javadoc for class indicates that both onCreateView(...) methods return null as the default behavior, and that the pre API 11 method has an empty implementation.

like image 87
user5292387 Avatar answered Sep 27 '22 16:09

user5292387


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

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

For more information and take a decision look Android Lint Checks: HERE

Also you can use @TargetApi.

The difference is that with @TargetApi, you declare, via the parameter, what API level you have addressed in your code, so that the error can pop up again if you later modify the method to try referencing something newer than the API level cited in @TargetApi.

@TargetApi is better annotation, allows you to tell the build tools "OK, I fixed this category of problems" in a more fine-grained fashion.

like image 26
josedlujan Avatar answered Sep 27 '22 17:09

josedlujan