bug in Fragments setArguments and getArguments

0

I'm trying to pass some information from ActivityMain to a fragement but the android is complaining, in ActivityMain I pass values like this:

if (f_caixaTexto== null) {
        f_caixaTexto = new PageCaixaTextoFragment();
        f_caixaTexto.setArguments(Constant.getDuvidas(this));

}
adapter.addFragment(f_restituicao, "Duvidas");
viewPager.setAdapter(adapter);

Now in my Fragemento I get the data like this:

public PageCaixaTextoFragment() {
      Bundle bundle = this.getArguments();
      CaixaTexto caixaTexto = (CaixaTexto) bundle.getSerializable("bundle");
      titulo_texto = caixaTexto.getTitulo();
      corpo_texto = caixaTexto.getCorpo();
      imagem_int = caixaTexto.getImagem();
}

The getDuvidas (this) method has the following code

public static Bundle getDuvidas(Context ctx){
       Bundle bundle = new Bundle();
       String titulo = ctx.getResources().getString(R.string.duvidas_titulo);
       String corpo = ctx.getResources().getString(R.string.duvidas);
       int imagem = R.drawable.ic_group_banco;
       CaixaTexto tmp = new CaixaTexto(titulo,imagem,corpo);
       bundle.putSerializable("bundle",tmp);
       return bundle;
}

But when I try to run I get the following error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference

Where am I going wrong?

    
asked by anonymous 09.03.2017 / 00:10

1 answer

3

The getArguments function should be called after the fragment is created, typically in the onCreate method doing for example:

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

    Bundle bundle = this.getArguments();
}
    
09.03.2017 / 01:55