问题
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.FragmentActivity
like this and error goes away, I want to be sure if this is the way to go.
回答1:
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.
回答2:
@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.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/32294607/call-requires-api-level-11current-min-is-9-android-app-activityoncreateview