-1

this is my android code when we run application and it is automatically close when login method is call on the clicking of the button

FATAL EXCEPTION: main Process: soubhagya.hostinger, PID: 25611 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:5640)
at android.view.View$PerformClick.run(View.java:22455)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5640) 
at android.view.View$PerformClick.run(View.java:22455) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6165) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at soubhagya.hostinger.Login.logIn(Login.java:51)
at soubhagya.hostinger.Login.radhaJi(Login.java:106)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  at android.view.View.performClick(View.java:5640) 
at android.view.View$PerformClick.run(View.java:22455) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6165) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

Here's the Login activity.

public class Login extends AppCompatActivity {

    //web url string
    public static final String LOGIN_URL="http://abhinavin.000webhostapp.com/userregistration/login.php";
    public static final String KEY_EMAIL="email";
    public static final String KEY_PASSWORD="password";
    public static final String LOGIN_SUCCESS="success";
    public static final String SHARED_PREF_NAME="tech";
    public static final String EMAIL_SHARED_PREF ="email";
    public static final String LOGGEDIN_SHARED_PREF="loggedin";
    private EditText editTextEmail;
    private EditText editTextPassword;
    private Button btn_SignIn;
    private boolean loggedIn=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //this is text fields
        editTextEmail=findViewById(R.id.email);
        editTextPassword=findViewById(R.id.password);
        btn_SignIn=findViewById(R.id.btn_signup);

    }
    //End of onCreate method
this login method is call on the click of button

    private void logIn() {
        //error is in this line i think.
        //get value of email from edit text
        final String email = editTextEmail.getText().toString();
        //get value of password from edit text
        final String password = editTextPassword.getText().toString();
        StringRequest stringRequest=new StringRequest(Request.Method.POST, LOGIN_URL, new Response.Listener<String>() {
            //override the onResponse method
            @Override
            public void onResponse(String response) {

                 //check condition
                if (response.trim().equalsIgnoreCase(LOGIN_SUCCESS)) {
                    SharedPreferences sharedPreferences = Login.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean(LOGGEDIN_SHARED_PREF, true);

                    editor.putBoolean(EMAIL_SHARED_PREF, Boolean.parseBoolean(email));
                    editor.commit();
                    Intent i = new Intent(Login.this, MainActivity.class);
                    startActivity(i);
                } else {
                    Toast.makeText(Login.this, "Invalid password", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }){
            //overridre getParams
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> prams=new HashMap<>();
                //put the data in map
                prams.put(KEY_EMAIL, email);
                prams.put(KEY_PASSWORD,password);
                //return prams
                return prams;
            }


        };
        RequestQueue requestQueue= Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
End of login method

    //overtide the onResume method
    @Override
    protected void onResume() {
        super.onResume();
        //get SharedPreferences
        SharedPreferences sharedPreferences=getSharedPreferences(SHARED_PREF_NAME,Context.MODE_PRIVATE);
        loggedIn=sharedPreferences.getBoolean(LOGGEDIN_SHARED_PREF ,false);
        if(loggedIn)
        {
            //set Intent object
            Intent i=new Intent(Login.this, MainActivity.class);
            //StartActivity
            startActivity(i);
        }
    }
    //this is onclick function
    public void btn_Click(View view) {
        //call login function
        logIn();

    }
}

xml code in this code edit text and button are defined

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="soubhagya.hostinger.Login">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editText3" android:hint="Emile"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/editText4" android:hint="Password"/>
<Button
        android:text="Signin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:id="@+id/button3"
        android:onClick="btn_Click"
/></LinearLayout>

this is my xml code in whis we can design ui of our application

John Joe
  • 12,412
  • 16
  • 70
  • 135

2 Answers2

0
  1. You should change to btn_Click in your xml

from android:onClick="radhaJi"

to android:onClick="btn_Click".

  1. Your button initialize wrong id

    btn_SignIn=findViewById(R.id.btn_signup);
    

should be

 btn_SignIn=findViewById(R.id.button3);
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

You need implement the View.OnClickListener along with your Activity like the following.

public class Login extends AppCompatActivity implements View.OnClickListener {

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_signup)
            logIn();
    }
}

And change the layout to look like the following.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Emile"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btn_signup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Signin" />
</LinearLayout>
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98