Fragments setArguments and getArguments error

Asked

Viewed 55 times

0

I’m trying to pass some information from Activitymain to a fragement but android is complaining, in Activitymain I pass the values so:

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

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

already in my Fragemento I receive 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 method getDuvidas(this) 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 I’m going wrong?

1 answer

3


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

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

    Bundle bundle = this.getArguments();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.