Error non-default constructors in fragments

后端 未结 5 2147
长发绾君心
长发绾君心 2020-11-28 14:35

I am dealing with maps apiv2. And I am getting the following error while coding for Dialog Fragment class.

Error : Avoid non-de

相关标签:
5条回答
  • 2020-11-28 15:10

    if you like to be out of rules just do next

    @SuppressLint("ValidFragment")
    public PlaceDialogFragment(Place place, DisplayMetrics dm){
            super();
            this.mPlace = place;
            this.mMetrics = dm;
        }
    
    0 讨论(0)
  • 2020-11-28 15:15

    The easier way is to add these to the gradle:

    android {
          lintOptions {
              checkReleaseBuilds false
          }
      }
    

    Or you can add this line in each fragment

    @SuppressLint("ValidFragment")
    public PlaceDialogFragment(Place place, DisplayMetrics dm){
        super();
        this.mPlace = place;
        this.mMetrics = dm;
    }
    

    Best solution is add lines in gradle simply.

    0 讨论(0)
  • 2020-11-28 15:16
    @SuppressLint({"NewApi", "ValidFragment"})
    public PlaceDialogFragment(Place place, DisplayMetrics dm){
            super();
          }
    
    0 讨论(0)
  • 2020-11-28 15:21

    solution worked for me

    android {
              lintOptions {
                  checkReleaseBuilds false
              }
          }
    
    0 讨论(0)
  • 2020-11-28 15:28

    Because of the nature of fragment and how there managed and shown on your screen it's very recommended not to create a non default constructor and in many cases this would cause problems in run time. In case you should do something like this:

    public static final GridFragment newInstance(String tabId)
    {
        GridFragment f = new GridFragment();
        Bundle bdl = new Bundle(2);
        bdl.putString(TAB_ID, tabId);
        f.setArguments(bdl);
        return f;
    }
    

    and in onCreate extract the needed data from the bundle:

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        String tabId = getArguments().getString(TAB_ID);
        }
    

    And take a look at this question as well:

    Do fragments really need an empty constructor?

    0 讨论(0)
提交回复
热议问题