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

After updating the SDK (23), I get this lint error, I did not make any changes to my code, and it worked 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 autofix button, it will add @SuppressLint ("NewApi") to the class @SuppressLint("NewApi") public class MyActivity extends android.support.v4.app.FragmentActivity , like this, and the error @SuppressLint("NewApi") public class MyActivity extends android.support.v4.app.FragmentActivity away, I want Be sure that this is the way to go.

+7
android lint
source share
4 answers

I ran into the same problem.

If you 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 will see that the public View onCreateView method (View parent, String name, context context, AttributeSet attrs) was added in API 11.

Instead of using @SuppressLint ("NewApi") at the class declaration level, I added this particular method to my code and suppressed the lint warning to declare the method. For example:

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

Thus, any future additions to the class code will still be checked by lint, but lint will fail to flag this method with an error.

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

+6
source share

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

Something in your code is not optimal or may cause a crash. By skipping NewApi, you suppress all warnings that tell you if you are using any API entered after your minSdkVersion

For more information and to make a decision, see Android Lint Checks: HERE

You can also use @TargetApi.

The difference is that with @TargetApi you declare through the parameter which API level you specified in your code so that the error can appear again if you later change the method to try to refer to something new than the API level specified in @TargetApi.

@TargetApi - the best annotation, allows you to tell the build tools "OK, I fixed this category of problems" more subtly.

+1
source share

As mentioned by user5292387, oncreateview has been added. Instead of suppressing 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 super call is for devices running Honeycomb Android OS and above. The second challenge is super - for devices with less than Honeycomb Android OS. I think it looks cleaner rather than returning null. However, the android documentation states that returning null will result in a default behavior. Any solution should work, however I am skeptical about returning null, as this can have adverse effects in later versions of the Android SDK.

0
source share

Something that seems all missing is that it uses the FragmentActivity from the v4 support library. By definition, this class should be fully compatible with Android API 4. A warning should not be issued, because FragmentActivity provides its own implementation of onCreateVivew ().

It seems to me that this is a Lint bug.

I think @SupressLint ("NewAPI") is the easiest way to get around a Lint error (since I don't consider it a mistake at all). Also remember that Lint errors are not compilation errors. They suggest that you may have code problems, or there is a better way to do this.

0
source share

All Articles