Press "Enter" to skip to content

Error “Could not find method in a parent or ancestor Context” how do i fix this?

Last updated on December 24, 2021

i’m trying to send some string from edittext that is located on my first activity into the second activity by pressing Button and i want to see this string in the edittext on that second activity, maybe somebody could help me please

My onClick method:

public void calendarOR(View v, AdapterView<?> parent, int position, long id) {
            String data = cala.getText().toString();
            Intent intent = new Intent(getApplicationContext(), calendar.class);
            intent.putExtra("id",data);
            startActivity(intent);

The second activity:

  Bundle extras = getIntent().getExtras();
    if (extras != null) {
        userId = extras.getLong("id");
    }
    if (userId > 0) {
        userCursor = db.rawQuery("select * from " + dbelper.TABLE + " where " + dbelper.COLUMN_ID + "=?", new String[]{String.valueOf(userId)});
        userCursor.moveToFirst();
        date.setText(userCursor.getString(1));
        userCursor.close();
    }

But i get an Error:

        java.lang.IllegalStateException: Could not find method calendarOR(View) in a parent or 
        ancestor Context for android:onClick attribute defined on view class 
        com.google.android.material.button.MaterialButton with id 'buttonCal2'