Retrieve past information in a Bundle

Asked

Viewed 412 times

0

I have a Fragment that calls another Fragment by passing login information to the other. I do this with the following code through the Bundle. Follow the code :

ImageView editarBt = (ImageView) view.findViewById(R.id.editar);
            editarBt.setOnClickListener(new Button.OnClickListener(){
                @Override
                public void onClick(View view) {

                    Bundle data = new Bundle();
                    data.putString("nome", listaUser.get(auxPosition).getNome());
                    data.putString("email", listaUser.get(auxPosition).getEmail());
                    data.putString("np", listaUser.get(auxPosition).getNp());
                    data.putString("tipoFunc", listaUser.get(auxPosition).getTipoFunc());
                    data.putInt("id", listaUser.get(auxPosition).getId());
                    Fragment fragment = new adicionar_usuario();
                    fragment.setArguments(data);

                    FragmentManager manager = getFragmentManager();
                    manager.beginTransaction().replace(R.id.content,fragment,fragment.getTag()).addToBackStack(null).commit();

                }
            });

The problem is I’m having trouble capturing that data in the other fragment. I managed to do this with Intent when I have activitys, with the following code:

Intent intent = getActivity().getIntent();
    if(intent != null){
        Bundle bundle = getArguments();
        if(bundle != null){
            int id = bundle.getInt("id");
            String nome = bundle.getString("nome");
            String email = bundle.getString("email");
            String tipoFunc = bundle.getString("tipoFunc");
            String np = bundle.getString("np");

            usuario.setId(id);
            usuario.setNome(nome);
            usuario.setEmail(email);
            usuario.setTipoFunc(tipoFunc);
            usuario.setNp(np);

            nomeEt.setText(usuario.getNome());
            emailEt.setText(usuario.getEmail());
            npEt.setText(usuario.getNp());
            tipoFuncSp.toString();

            senhaEt.setVisibility(View.GONE);
            salvarBt.setVisibility(View.GONE);
            editarUserBt.setVisibility(View.VISIBLE);


        }
    }

How to transform this code to a Fragment? Making the user field load with user information by clicking the edit button.

You have the following error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
                                                                                                   at com.example.gerdaumanagement.gerdaumanagement.adicionar_usuario.onCreateView(adicionar_usuario.java:116)
                                                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                                                   at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                                                   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

1 answer

3


To recover data from bundle, use the method getAguments(). See below for an example:

@Override
public View onCreateView(...) { 
    .
    .
    Bundle bundle = getArguments();
    if (bundle != null) {
        int id = bundle.getInt("id");
    }
  • @acklay I edited as you informed me, but is giving error. I edited the question.

  • Debug the expensive code. Check if Intent is really giving null fierent, otherwise it will not enter within the condition if(intent != null)

  • Intent is equal to null

  • @Lucascharles so was not entering the condition. = D hehehe

  • Because it’s haha now I’m trying to see pq. When I’m swallowing my information is correct, but when I pass it, it goes like null to the other fragment. Fragment fragment = new adicionar_usuario();
 fragment.setArguments(data);

 FragmentManager manager = getFragmentManager();
 manager.beginTransaction().replace(R.id.content,fragment,fragment.getTag()).addToBackStack(null).commit();

  • @Lucascharles Which Intent? What do you get with getArguments() is a Bundle. On the other hand the error posted in the question refers to the fact that an Edittext is null and not the Bundle. Edit the question and enter the method code onCreateView().

  • @acklay Thank you so much!

  • @Did Lucascharles work right?! Need anything else, a touch and/or ask a question. The people here in the O.R.Most of the time like to help.

  • @acklay did work :)

Show 4 more comments

Browser other questions tagged

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