0
My problem is this, I have my main screen that contains a button to access another screen, the code I’m using to make this call is:
btRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.activity_register);
}
});
It turns out that inside this new screen(activity_register) i have several fields(Edittext) and two buttons, Register and Cleanse, the task of the button Register is that when pressed it collects the values of the fields(Edittext) and enter in the String, for this I am using the following code:
Part 1:
EditText etRegisterName,etRegisterSecondName,etRegisterAge,etRegisterEmail,etRegisterPhone;
Part 2:
name = etRegisterName.getText().toString();
secondname = etRegisterSecondName.getText().toString();
age = etRegisterAge.getText().toString();
email = etRegisterEmail.getText().toString();
phone = etRegisterPhone.getText().toString();
In short, my Mainactivy this way:
public class MainActivity extends ActionBarActivity {
EditText etSearch;
EditText etRegisterName,etRegisterSecondName,etRegisterAge,etRegisterEmail,etRegisterPhone;
Button btSearch,btRegister,btRegisterFinish;
String name, secondname, age, email, phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etSearch = (EditText) findViewById(R.id.Search);
btSearch = (Button) findViewById(R.id.Search_Button);
btRegister = (Button) findViewById(R.id.Register_Button);
btRegisterFinish = (Button) findViewById(R.id.btRegisterFinish);
etRegisterName = (EditText) findViewById(R.id.etRegisterName);
etRegisterSecondName = (EditText) findViewById(R.id.etRegisterSecondName);
etRegisterAge = (EditText) findViewById(R.id.etRegisterAge);
etRegisterEmail = (EditText) findViewById(R.id.etRegisterEmail);
etRegisterPhone = (EditText) findViewById(R.id.etRegisterPhone);
btSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String busca = etSearch.getText().toString();
String tableTest = name+secondname+age+email+phone;
String[][] table = {{"Rosana","De Oliveira","39","[email protected]","(00) 5555-5555"},
{"Jackson","De Almeida","15","[email protected]","(00) 6666-66666"}};
for(int i=0;i < table.length;i++){
for(int j=0;j < table[i].length;j++){
if(busca.contains(table[i][j]) == true){
AlertDialog.Builder searchOK = new AlertDialog.Builder(MainActivity.this);
searchOK.setTitle("Busca Concluida");
searchOK.setMessage("Nome: " + table[i][0] + "\n"
+ "Sobrenome: " + table[i][1] + "\n"
+ "Idade: " + table[i][2] + "\n"
+ "E-mail: " + table[i][3] + "\n"
+ "Telefone: " + table[i][4]);
searchOK.setNeutralButton("Fechar", null);
searchOK.show();
}
}
}
}
});
btRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.activity_register);
}
});
btRegisterFinish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = etRegisterName.getText().toString();
secondname = etRegisterSecondName.getText().toString();
age = etRegisterAge.getText().toString();
email = etRegisterEmail.getText().toString();
phone = etRegisterPhone.getText().toString();
AlertDialog.Builder confirmRegister = new AlertDialog.Builder(MainActivity.this);
confirmRegister.setTitle("Dados do Registro:");
confirmRegister.setMessage("Nome: " + name + "\n"
+ "Sobrenome: " + secondname + "\n"
+ "Idade: " + age + "\n"
+ "E-mail: " + email + "\n"
+ "Telefone: " + phone);
confirmRegister.setNeutralButton("Alterar", null);
confirmRegister.setPositiveButton("Registrar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setContentView(R.layout.activity_main);
AlertDialog.Builder alertSucess = new AlertDialog.Builder(MainActivity.this);
alertSucess.setTitle("Registrado!");
alertSucess.setMessage("Registro feito com Sucesso!");
alertSucess.setNeutralButton("OK", null);
}
});
confirmRegister.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My problem is that when I try to access this screen(activity_register) of error saying that the application has stopped! I can’t even access another screen where this button completes the Log, does anyone have any idea what it might be? if the question got confused I’m sorry, leave in the comments I will try to explain better!
Jeiferson, it is not recommended to call the
setContentView
more than once for a certainActivity
, although it works, it can be difficult to manage references by making this exchange. Why not start anotherActivity
or even useFragment
to present a new canvas? This question may clarify a few details: http://stackoverflow.com/questions/22227950/android-is-using-setcontentview-multiple-times-bad-while-changing-layouts.– Wakim
I’ll try, but I don’t start another way to call screen unless using the setContent...
– Jeiferson
You can start a new
Activity
using aIntent
. I think these questions may help you: http://answall.com/questions/11455/chamar-nova-activity-atraves-de-umaimagemview, http://answall.com/questions/42346/como-aber-uma-activity-j%C3%A1-open-before and http://pt.stackoverflowcom/questions/17483/qual-a-difference%C3%A7a-between-draw-na-Activity-e-no-Fragment– Wakim
A while ago, I went through a very similar situation. I suggest reading this question here: http://answall.com/questions/17633/
– emanuelsn
@emanuelsn, I did a test, I removed the code from the button "Register that this located in activity_register and worked normally, called the screen, then I walked thinking, each screen has to have its class . java to manage it?
– Jeiferson
Exact. Each Activity (screen) must have its class so that you identify the screen components, click/touch actions and etc. Remember, each Activity must be declared in the Android Manifest, if not, it will cause a failure in the . apk.
– emanuelsn
Yes, I was putting the stock code of the other Activity in activity_main, so it didn’t work, vlw!
– Jeiferson