AppCompatActivity.onCreate can only be called from within the same library group

℡╲_俬逩灬. 提交于 2019-11-26 06:32:58

问题


After upgrading to appcompat 25.1.0 I\'ve started getting wired errors.

In my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

I get lint error:

AppCompatActivity.onCreate can only be called from within the same library group (groupId=com.android.support)

How to prevent such behavior?


回答1:


As Felipe already pointed out in his comment this is a bug in the pre-release version of the tools.

You can workaround it for now, until Google release a fix, by adding the following into your project module's build.gradle file:

android {
  lintOptions {
    disable 'RestrictedApi'
  }
}

It's worth noting that this may hide true errors in your project as it suppresses all errors of that type, so the better option would be to downgrade the version of Android Studio and the tools used in the project.




回答2:


As previous responses highlighted, it is bug. I recommend not to disable the specific lint warning project-wide, but for that method only. Annotate your method as follows:

@SuppressLint("RestrictedApi")
@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    //your code here
}



回答3:


Disabling the warning in lintOptions doesn't look a good option it's better to suppress inspection at the statement level.

Add this comment above the line of code which gives the warning:

//noinspection RestrictedApi


来源:https://stackoverflow.com/questions/41150995/appcompatactivity-oncreate-can-only-be-called-from-within-the-same-library-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!