8

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.

user65721
  • 2,833
  • 3
  • 19
  • 28
  • Possible duplicate of [Call requires API level 11(current min is 8) android.app.Activity#onCreateView](http://stackoverflow.com/questions/32181705/call-requires-api-level-11current-min-is-8-android-app-activityoncreateview) – Murmel Oct 18 '15 at 21:32

4 Answers4

7

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.

user5292387
  • 413
  • 2
  • 7
  • This is a non-answer because their code does not call/override `onCreateView` at all. It is a bug in the Android SDK; [here is the issue to star](https://code.google.com/p/android/issues/detail?id=188677). – nkorth Oct 14 '15 at 17:42
  • @nkorth, `onCreateView` is called by android 'when inflating with the LayoutInflater returned by getSystemService(Class).' The api docs say so, right there, in the link I provided which you didn't bother to read. Second, the question was about the use of @SuppressLint to suppress the warning, not about the source of the bug. But kudos to you for finding a reference to it. – user5292387 Oct 21 '15 at 13:28
1

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

josedlujan
  • 5,357
  • 2
  • 27
  • 49
  • Actually I do not want to use annotations at all and I also do not need them because my code does not use Activity#onCreateView at all. Error is raised because of android api itself. I am trying to find best workaround for android's own bug. Your answer is helpful, and thank you for that, however it does not answer my actual question. – user65721 Aug 30 '15 at 11:14
0

As mentioned by user5292387 the oncreateview was added. Instead of suppressing the lint I used

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) 
{
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
        super.onCreateView(parent, name, context, attrs) : 
        super.onCreateView(name, context, attrs);   
}

The first call to super is for devices that are running Honeycomb Android OS and greater. The second call to super is for devices running less than Honeycomb Android OS. I think it looks cleaner instead of returning null. However the android documentation does state that returning null will result in default behavior. Either solution should work, however I am skeptical about returning null since this might have adverse effects in later releases of the Android SDK.

SavageKing
  • 556
  • 4
  • 16
0

Something that everyone appears to be missing is that he is using FragmentActivity from the v4 Support Library. By definition, this class is supposed to be compatible all the way back to Android API 4. No warning should be issued as FragmentActivity provides its own implementation of onCreateVivew().

It appears to me this is a Lint bug.

I think the @SupressLint("NewAPI") is the easiest way to get around the Lint error (as I don't believe it's an error at all). Also remember that Lint errors aren't compilation errors. They are suggestions to you that you may have issues in your code or there is a better way to do it.

Alther
  • 451
  • 4
  • 6