I have a Fragment that calls another fragment passing login information to the other. I do this with the following code through the Bundle. Here is 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 that I am not able to capture this data in the other fragment. I was able 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 turn this code into a Fragment? Making the user field load 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)