简体   繁体   English

调用需要API级别11(当前最小值为9)android.app.Activity#onCreateView

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

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. 在SDK更新(23)之后,我收到了这个lint错误,我没有对我的代码进行任何更改,它在api级别为9的设备上工作正常。此外,我不调用android.app.Activity#onCreateView in我的代码。 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. 如果我单击自动修复,它将@SuppressLint(“NewApi”)放到类@SuppressLint("NewApi") public class MyActivity extends android.support.v4.app.FragmentActivity就像这样,错误就消失了,我我想确定这是否可行。

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. 如果你看一下Activity类的javadoc( http://developer.android.com/reference/android/app/Activity.html#onCreateView%28android.view.View,%20java.lang.String,%20android .content.Context,%20android.util.AttributeSet%29 ),您将看到API 11中添加了方法public View onCreateView(View parent,String name,Context context,AttributeSet attrs)

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. 我没有在类声明级别使用@SuppressLint(“NewApi”),而是将特定方法添加到我的代码中并抑制了方法声明的lint警告。 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. 这样,类的代码的任何未来添加仍将由lint检查,但lint将停止标记此方法并出现错误。

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. ETA:类的Javadoc指示onCreateView(...)方法都返回null作为默认行为,并且pre API 11方法具有空实现。

@SuppressLint("NewApi") is an annotation used by the Android Lint tool. @SuppressLint(“NewApi”)是Android Lint工具使用的注释。

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 通过在那里传递NewApi,您将抑制所有警告,告诉您是否使用了minSdkVersion之后引入的任何API

For more information and take a decision look Android Lint Checks: HERE 有关更多信息并做出决定,请查看Android Lint Checks: HERE

Also you can use @TargetApi. 你也可以使用@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,您可以通过参数声明您在代码中处理的API级别,以便稍后修改方法以尝试引用比API中引用的API级别更新的错误时再次弹出错误@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. @TargetApi是更好的注释,允许你以更细粒度的方式告诉构建工具“OK,我修复了这类问题”。

As mentioned by user5292387 the oncreateview was added. 如user5292387所述,添加了oncreateview。 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. 对super的第一次调用是针对运行Honeycomb Android OS及更高版本的设备。 The second call to super is for devices running less than Honeycomb Android OS. 第二次调用super是针对运行少于Honeycomb Android OS的设备。 I think it looks cleaner instead of returning null. 我认为它看起来更干净而不是返回null。 However the android documentation does state that returning null will result in default behavior. 然而,android文档确实声明返回null将导致默认行为。 Either solution should work, however I am skeptical about returning null since this might have adverse effects in later releases of the Android SDK. 两种解决方案都应该有效,但是我对返回null持怀疑态度,因为这可能会对Android SDK的后续版本产生负面影响。

Something that everyone appears to be missing is that he is using FragmentActivity from the v4 Support Library. 每个人似乎都缺少的是他正在使用v4支持库中的FragmentActivity。 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(). 根据定义,该类应该一直兼容到Android API 4.由于FragmentActivity提供了自己的onCreateVivew()实现,因此不应发出警告。

It appears to me this is a Lint bug. 在我看来,这是一个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). 我认为@SupressLint(“NewAPI”)是解决Lint错误的最简单方法(因为我根本不相信这是一个错误)。 Also remember that Lint errors aren't compilation errors. 还要记住,Lint错误不是编译错误。 They are suggestions to you that you may have issues in your code or there is a better way to do it. 他们向您建议您可能在代码中遇到问题,或者有更好的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用需要API级别11(当前最小值为8)android.app.Activity#onCreateView - Call requires API level 11(current min is 8) android.app.Activity#onCreateView 调用需要API级别11(当前最小值为8):android.app.Activity#onCreateView FragmentActivity - Call requires API level 11 (current min is 8): android.app.Activity#onCreateView FragmentActivity 调用需要API级别11(当前最小值为8):android.app.Activity#invalidateOptionsMenu - Call requires API level 11 (current min is 8): android.app.Activity#invalidateOptionsMenu 调用需要API级别11(当前最小值为1) - Call requires API level 11 (current min is 1) 调用需要API级别11(当前最小值为8):new android.app.AlertDialog.Builder - Call requires API level 11 (current min is 8): new android.app.AlertDialog.Builder 呼叫需要API级别14(当前最小值为11) - Call requires API level 14 (current min is 11) Ripple需要API级别21(当前最小值为11),android? - Ripple requires API level 21 (current min is 11), android? setAdapter“调用需要API级别11(当前最小值为8):android.widget.AbsListView #setAdapter”? - setAdapter “Call requires API level 11 (current min is 8): android.widget.AbsListView#setAdapter”? android:editTextBackground需要API级别11(当前最小值为7) - android:editTextBackground requires API level 11 (current min is 7) ?android:attr / selectableItemBackground`需要API级别11(当前最小值为10) - ?android:attr/selectableItemBackground` requires API level 11 (current min is 10)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM