'Could not find method error when setting onClick function?

 <RelativeLayout
        android:id="@+id/btn_login"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_below="@id/password_bar"
        android:layout_centerInParent="true"
        android:layout_marginTop="70dp"
        android:background="@drawable/login_signup_btn_bg"
        android:clickable="true"
        android:onClick="login">

        <TextView
            android:id="@+id/tx_btn_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:fontFamily="@font/poppins_regular"
            android:onClick="login"
            android:text="Log In"
            android:textColor="@color/white"
            android:textSize="20sp" />
    </RelativeLayout>
package com.jamdev.handmedown;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;


import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;

public class LoginActivity extends AppCompatActivity {

    private String URL = "http://10.0.2.2/HandMeDown/user_login.php";
    public String username;
    public String password;
    public EditText usernameInput;
    public EditText passwordInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // These 3 lines hide the title and action bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_login);
        usernameInput = (EditText) findViewById(R.id.username_input);
        passwordInput = (EditText) findViewById(R.id.password_input);


    }
            public void login(View view){
            userAuthenticationAPI user_authentication_api = new userAuthenticationAPI();
            user_authentication_api.execute();
        }

      class userAuthenticationAPI extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            HttpClient http_client = new DefaultHttpClient();
            HttpPost http_post = new HttpPost(URL);

            username = usernameInput.getText().toString();
            password = passwordInput.getText().toString();

            BasicNameValuePair usernameParam = new BasicNameValuePair("Username", username);
            BasicNameValuePair passwordParam = new BasicNameValuePair("Password", password);

            ArrayList<NameValuePair> name_value_pair_list = new ArrayList<>();
            name_value_pair_list.add(usernameParam);
            name_value_pair_list.add(passwordParam);

            try {
                // Send the list of name value pairs using an encoded form entity
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(name_value_pair_list);

                // Set the post object to contain the entity
                http_post.setEntity(entity);

                // Reads API response and string
                HttpResponse http_response = http_client.execute(http_post);
                InputStream input_stream = http_response.getEntity().getContent();
                InputStreamReader input_stream_reader = new InputStreamReader(input_stream);
                BufferedReader buffered_reader = new BufferedReader(input_stream_reader);
                StringBuilder string_builder = new StringBuilder();
                String buffered_str_chunk = null;
                while ((buffered_str_chunk = buffered_reader.readLine()) != null) {
                    string_builder.append(buffered_str_chunk);
                }
                Log.i("result", string_builder.toString());
                return string_builder.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONObject json = new JSONObject(s);
                // Returned status from the post API
                String status = json.getString("status");

                if (status.equalsIgnoreCase("accepted")) {
                    String fullName = json.getString("Name");
                    String phoneNumber = json.getString("PhoneNumber");
                    String address = json.getString("Address");
                    String username = json.getString("Username");
                    String email = json.getString("Email");
                    int profilePicture = R.drawable.profile_pic;

                    User user = new User(fullName,phoneNumber,address,username,email,profilePicture);

                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("User", user);
                    startActivity(intent);
                } else {
                    // If the status != accepted, the user is notified that something wrong happened
                    Toast.makeText(LoginActivity.this, status, Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        
    }



}

The app is crashing whenever I click on the log in button. I tried removing the content inside, and just trying to make a Toast and display whatever message is inside, not budging, so it can't be from the api execute function call. I'm getting this error:

java.lang.IllegalStateException: Could not find method login(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.RelativeLayout with id 'btn_login'

It's driving me nuts. I made sure it's within its scope, and when selecting the onClick function it gives me the login function as an option.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source