6
I’m calling a method, when I click the button and this error appears, but I don’t know what it means:
java.lang.Illegalstateexception: Could not find method to schedule(View) in a Parent or Ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.Appcompatbutton with id 'btnAgendar'
Follows my code
public class AgendamentoActivity extends AppCompatActivity {
private ProfissionalPojo p;
private ListView listAgendamentos;
private Spinner spinnerHorario;
private String dadoHora;
private StringCharacterIterator editText;
private Calendar myCalendar;
private String diaAgenda;
private int idusuario;
private String nome;
private String sobrenome;
private String dia;
private String hora;
private Button botaoAgenda;
//private horario adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agendamento);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle extra = getIntent().getExtras();
if(extra != null){
idusuario = extra.getInt("idusuario");
nome = extra.getString("nome");
sobrenome = extra.getString("sobrenome");
}
Toast.makeText(this, "id usuario "+idusuario, Toast.LENGTH_SHORT).show();
final Calendar myCalendar = Calendar.getInstance();
p = (ProfissionalPojo) getIntent().getSerializableExtra("pro");
agendar(p.getIdprofissional(), idusuario, nome, sobrenome, dia, hora);
listAgendamentos = ((ListView)findViewById(R.id.lvAgendamentos));
botaoAgenda = ((Button) findViewById(R.id.btnAgendar));
EditText editText = ((EditText)findViewById(R.id.edtDia));
spinnerHorario = (Spinner) findViewById(R.id.spHorario);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spHorario, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerHorario.setAdapter(adapter);
spinnerHorario.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(i == 0){
//Toast.makeText(getBaseContext(), "8:00 - 9:00", Toast.LENGTH_LONG).show();
String dadoHora = "8:00 - 9:00";
}else if (i == 1){
//Toast.makeText(getBaseContext(), "9:00 - 10:00", Toast.LENGTH_LONG).show();
String dadoHora = "9:00 - 10:00";
}else if (i == 2){
//Toast.makeText(getBaseContext(), "10:00 - 11:00", Toast.LENGTH_LONG).show();
String dadoHora = "10:00 - 11:00";
}else if (i == 3){
//Toast.makeText(getBaseContext(), "11:00 - 12:00", Toast.LENGTH_LONG).show();
String dadoHora = "11:00 - 12:00";
}else if (i == 4){
//Toast.makeText(getBaseContext(), "12:00 - 13:00", Toast.LENGTH_LONG).show();
String dadoHora = "12:00 - 13:00";
}else if (i == 5){
//Toast.makeText(getBaseContext(), "13:00 - 14:00", Toast.LENGTH_LONG).show();
String dadoHora = "13:00 - 14:00";
}else if (i == 6){
//Toast.makeText(getBaseContext(), "14:00 - 15:00", Toast.LENGTH_LONG).show();
String dadoHora = "14:00 - 15:00";
}else if (i == 7){
//Toast.makeText(getBaseContext(), "15:00 - 16:00", Toast.LENGTH_LONG).show();
String dadoHora = "15:00 - 16:00";
}else if (i == 8){
//Toast.makeText(getBaseContext(), "16:00 - 17:00", Toast.LENGTH_LONG).show();
String dadoHora = "16:00 - 17:00";
}else if (i == 9){
//Toast.makeText(getBaseContext(), "17:00 - 18:00", Toast.LENGTH_LONG).show();
String dadoHora = "17:00 - 18:00";
}
dadoHora = spinnerHorario.getSelectedItem().toString();
exibeHora();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel(myCalendar);
}
};
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(AgendamentoActivity.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time = sdf.format(myCalendar.getTime());
}
public void exibeDia(){
final TextView tvDia = ((TextView) findViewById(R.id.tvDia));
tvDia.setText(diaAgenda);
dia = tvDia.toString();
}
public void exibeHora(){
final TextView tvHora = ((TextView) findViewById(R.id.tvHora));
tvHora.setText(dadoHora);
hora = tvHora.toString();
}
private void updateLabel(Calendar myCalendar) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
diaAgenda = sdf.format(myCalendar.getTime());
exibeDia();
}
public void agendar(final String idprofissional, int idusuario, String nome, String sobrenome, String dia, String hora) {
Log.d("TAG", "idpro " + idprofissional + " idusuario " + idusuario + " nome " +nome+ " sobrenome " +sobrenome+ " dia " + dia + " hora " + hora);
}
}
Perfect, but how do I pass the parameters it should "carry along"? id_professional, id_client, name...?
– GustavoSevero
is just you use normally within the method!
– Thiago Luiz Domacoski
exactly, calling attributes with this within the method.. i.e., this.id_client, this.nome... etc
– Gabriel Heguedusch
Blz, I get it... I’m following what you already said... In onCreate I just put the call of this method, right? But how should it look, because I’m trying to do this: schedule(p.getIdprofessional(), idusuario, first name, surname, day, time); and p.getIdprofessional() is underlined in red... See the print in the post above.
– GustavoSevero
no!!! If you are using the onclik do not need to add anything to oncreate
– Thiago Luiz Domacoski
All right, I’ll test.
– GustavoSevero
Closed! It worked kkkkkk
– GustavoSevero
So whenever I put the name of a method in onClick, in XML, I don’t need to put its call in onCreate, just create the method, right?
– GustavoSevero
Exact! put in onclick in xml is the same as set the setOnClickListener in the onCreate!
– Thiago Luiz Domacoski