Error when including a context within a static [duplicate] method

1

I am creating a newInstance within a fragment. The documentation says that it is like this

public static Clase newInstance(int index,CheckBox check) {
    Clase categories = new Clase ();

    java.util.Calendar now = java.util.Calendar.getInstance();

    TimePickerDialog tpd = new TimePickerDialog(
    this,
    now.get(java.util.Calendar.HOUR_OF_DAY),
    now.get(java.util.Calendar.MINUTE),
    check.isChecked()

    );

The problem is that calling the context this.getContext() or Clase.this tells me that the context can not be referenced within a static method.

Can someone help me with this? Since to be able to instantiate within a fragment I must do it with a static method, and in that class TimePickerDialog I need to pass the context.

    
asked by Eduardo Jesus 21.06.2016 в 23:36
source

1 answer

1

The documentation says that for a Fragment you have to create something similar to:

public static Fragment newInstance() 
{
    MyFragment myFragment = new MyFragment();
    return myFragment;
}

but regularly the Context used is that of the Activity being within a fragment:

getActivity()

You can use other methods but I think it's the right thing to get the context by getActivity() being within Fragment since also Activity extends from Context .

    
answered by 22.06.2016 в 00:25