-1

How to pass value from global variable application into fragment? I got null value error message.

Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

Global Variable

public class Gl extends Application {

    private String token = "bla bla";

    public void setToken(String token){
        this.token = token;
    }

    public String getToken() {
        return token;
    }
}

Fragment

public class MyFragment extends Fragment {

    private String token;

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

        token = ((Gl) getActivity().getApplicationContext()).getToken();
    }

  /* .... */

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Nurdin
  • 23,382
  • 43
  • 130
  • 308

4 Answers4

0

Check your AndroidManifest.xml file for definition of your Gl class

<application
    android:name=".Gl"

everything else looks good ...

Naveen Dew
  • 1,295
  • 11
  • 19
0

I think there is something wrong by default whit your intent. You should use different approach like to put the variable in res/string file and get it by

getString(R.string.you_string_id)

But if you want to pass some arguments to the fragment you ca do that as described here

MeLean
  • 3,092
  • 6
  • 29
  • 43
0

Firstly you must add G1 class to AndroidManifest.xml

<application android:name=".G1"
...
/>

And I advise you to use Dagger2 , for pass value into fragment.

0

Specify token of G1 class as public static