0
I want to pass the value of an Edittext (which is in a Fragment) to an attribute of an Activity object, but I am not succeeding, Toast from Activity returns null. Follow the code of the Fragment:
public class PerguntaUmFragment extends SlideFragment {
private PerguntaUmInterface perguntaUmInterface;
private EditText editMensagem;
public PerguntaUmFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_pergunta_um, container, false);
editMensagem = view.findViewById(R.id.editMensagem);
perguntaUmInterface.setMensagem(editMensagem.getText().toString());
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
perguntaUmInterface = (PerguntaUmInterface) context;
} catch (Exception e) {
Log.e("ERROR", e.toString());
}
}
public interface PerguntaUmInterface {
void setMensagem(String mensagem);
}
public void setPerguntaUmInterface(PerguntaUmInterface perguntaUmInterface) {
this.perguntaUmInterface = perguntaUmInterface;
}
}
Activity:
public class OcorrenciaActivity extends IntroActivity implements PerguntaUmFragment.PerguntaUmInterface {
private final Ocorrencia ocorrencia = new Ocorrencia();
@Override
protected void onCreate(Bundle savedInstanceState) {
Toast.makeText(OcorrenciaActivity.this,
"Mensagem: "+ ocorrencia.getMensagem(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
if (fragment instanceof PerguntaUmFragment) {
PerguntaUmFragment perguntaUmFragment = (PerguntaUmFragment) fragment;
}
}
@Override
public void setMensagem(String mensagem) {
ocorrencia.setMensagem(mensagem);
}
Because you just don’t send a Oast straight from the Fragment?
– Murillo Comino
No, Toast is just a way to check if the message arrived correctly, it just printed "null" on the screen.
– Bruno Oliveira