What is this "Could not find method schedule(View) in" error?

Asked

Viewed 1,479 times

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);

    }
    }

inserir a descrição da imagem aqui

3 answers

4


Error free translation:

java.lang.Illegalstateexception: Unable to find method schedule(View) in Class or Context for android: onClick attribute defined in the display class android.support.v7.widget.Appcompatbutton with id 'btnAgendar'

Then in the statement of xml of btnAgendar, must have something like:

android:onClick="agendar"

In order for the informed method to be found in the context, it must have the following characteristics::

  • Must be declared as public
  • You must possess as a paramêtro View
  • No return (void)

EXAMPLE

public void agendar(View view){
  agendar(p.getIdprofissional(), idUsuario, nome, sobrenome, dia, hora );
  // aqui voce podera acessar as propriedades da sua classe!
}

To View is the representation of the element clicked on the screen, in your case the btnAgendar.

This method must be declared in the Class reported as context(link in English) of xml.

 <RelativeLayout
 xmlns:tools="http://schemas.android.com/tools"
 tools:context=".seu.pacote.SuaActivity"
  ...>
  • Perfect, but how do I pass the parameters it should "carry along"? id_professional, id_client, name...?

  • is just you use normally within the method!

  • exactly, calling attributes with this within the method.. i.e., this.id_client, this.nome... etc

  • 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.

  • no!!! If you are using the onclik do not need to add anything to oncreate

  • All right, I’ll test.

  • Closed! It worked kkkkkk

  • 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?

  • 1

    Exact! put in onclick in xml is the same as set the setOnClickListener in the onCreate!

Show 4 more comments

3

You must have used in the XML button "Schedule" the attribute onClick pointing to the method agendar(). For this to work this way, the method has to be declared that way in Java:

public void agendar(View v)

If you want to use the method agendar() with the parameters you have defined, you have to remove the XML attribute and set the button System in the code, thus:

    botaoAgenda.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            agendar(p.getIdprofissional(), idusuario, nome, sobrenome, dia, hora);
        }
    });

And, of course, remove the direct call from agendar() in his onCreate() otherwise it will always be executed when Activity is created.

  • public void schedule(View v) must be inside or outside onCreate?

  • Always out, but as I said, the first way, you can’t pass parameters in the method. I recommend doing the second way, because the other way, if you want to rename or delete the method, you will always have to remember to also tamper with XML

  • I put the method call outside of onCreate and now a red underlined line appears and says "Missing method body, or declare Abstract"

  • Ta, I already have the method out of onCreate, but then I take the parameters of this method?

  • But isn’t the idea to call this method with those parameters that you were trying to pass every time you click the button? If so, do it the second way I told you.

  • Sorry, what’s the second way?

  • The one I posted in the reply.

Show 2 more comments

0

Here’s how it works for me:

I checked the file AndroidManifest.xml and the first activity that starts with the app (in my case, I wanted to start with the .LoginActivity, but it was .MainActivity by default), so I made the changes:

     <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.WhatsappClone">
            <activity android:name=".activity.CadastroActivity"></activity>
            <activity android:name=".activity.MainActivity" />
            <activity android:name=".activity.LoginActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

And in the classes LoginActivity.java and MainActivity.java I discovered a mistake in onCreate. In both classes the setContentView pointed to the same layout, so the error occurred:

"Could not find method login(View) in a Parent or Ancestor Context for android:onClick attribute defined on view class"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.