Well, I have a project that uses a navigation drawer, so the implements of MainActivity are already "busy", so many methods of doing this do not work in my case. I have a MainActivity that controls the fragments I use. In a fragment I have an EditText that will receive a name entered by the user and another that will receive this name and display it.
Fragment EditName
public class EditarNome extends Fragment implements View.OnClickListener{
private EditText editarNome;
private Button enviar;
String oi;
conexao con;
View rootview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_editar, container, false);
editarNome = (EditText) rootview.getRootView().findViewById(R.id.editarNome);
oi = editarNome.getText().toString();
return rootview;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
con = (conexao) Inicio);
enviar = (Button) rootview.findViewById(R.id.enviar);
enviar.setOnClickListener(this);
}
@Override
public void onClick(View view) {
con.resposta(oi);
}
}
Fragment Home (this is the activity I used to manage the Fragment HomeFragment)
public class Inicio extends FragmentActivity implements conexao {
protected void onCreate(Bundle savedInstaceState) {
super.onCreate(savedInstaceState);
setContentView(R.layout.inicio);
}
@Override
public void resposta(String data) {
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
InicioFragment frag = (InicioFragment) manager.findFragmentById(R.id.fragment);
frag.changeText(data);
}
}
Fragment HomeFragment (where I want to display the name)
public class InicioFragment extends Fragment {
TextView text;
View rootview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_main, container, false);
return rootview;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
text = (TextView) rootview.findViewById(R.id.campoData2);
}
public void changeText(String data) {
text.setText(data);
}
}
Connection Interface
public interface connection { public void response (String data); }
The error that Android says is in the fragment EditName, when I call Activity Home. I hope you can help me! Vlw