getActivity() returns null in Fragment function

后端 未结 15 1689
别那么骄傲
别那么骄傲 2020-11-22 07:28

I have a fragment (F1) with a public method like this

public void asd() {
    if (getActivity() == null) {
        Log.d(\"yes\",\"it is null\");
    }
}
         


        
15条回答
  •  盖世英雄少女心
    2020-11-22 08:06

    I have solved my problem this way.I have passed getApplicationContext from the previous class which has already access of getApplicationContext.I have passed Inputstream object to my new class Nutrients.

    try{
                        InputStream is= getApplicationContext().getAssets().open("nutrient_list.json");
                        Nutrients nutrients=Nutrients.getNutrients(topRecognition,is);
    
                      } catch (IOException e) {
                        e.printStackTrace();
                      }
    

提交回复
热议问题