App keep crashing (Login function is used)

Viewed 51

--------- beginning of system
--------- beginning of main
--------- beginning of crash
09-19 18:33:08.542  8788  8788 E AndroidRuntime: FATAL EXCEPTION: main
09-19 18:33:08.542  8788  8788 E AndroidRuntime: Process: com.example.basicaccount, PID: hidden
09-19 18:33:08.542  8788  8788 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.basicaccount/com.example.basicaccount.MainActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatEditText cannot be cast to com.rengwuxian.materialedittext.MaterialEditText
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3827)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4003)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:106)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:246)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:8625)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)09-19 18:33:08.542  8788  8788 E AndroidRuntime: Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatEditText cannot be cast to com.rengwuxian.materialedittext.MaterialEditText
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at com.example.basicaccount.MainActivity.onCreate(MainActivity.java:42)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:8207)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:8191)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3800)
09-19 18:33:08.542  8788  8788 E AndroidRuntime:        ... 11 more
09-19 18:35:36.177 18856 18856 E AndroidRuntime: FATAL EXCEPTION: main
09-19 18:35:36.177 18856 18856 E AndroidRuntime: Process: com.example.basicaccount, PID: hidden
09-19 18:35:36.177 18856 18856 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.basicaccount/com.example.basicaccount.MainActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatEditText cannot be cast to com.rengwuxian.materialedittext.MaterialEditText
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3827)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4003)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:106)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:246)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:8625)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)09-19 18:35:36.177 18856 18856 E AndroidRuntime: Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatEditText cannot be cast to com.rengwuxian.materialedittext.MaterialEditText
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at com.example.basicaccount.MainActivity.onCreate(MainActivity.java:42)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:8207)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:8191)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3800)
09-19 18:35:36.177 18856 18856 E AndroidRuntime:        ... 11 more
09-19 18:35:39.394 21952 21952 E AndroidRuntime: FATAL EXCEPTION: main
09-19 18:35:39.394 21952 21952 E AndroidRuntime: Process: com.example.basicaccount, PID: hidden
09-19 18:35:39.394 21952 21952 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.basicaccount/com.example.basicaccount.MainActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatEditText cannot be cast to com.rengwuxian.materialedittext.MaterialEditText
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3827)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4003)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:106)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:246)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:8625)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)09-19 18:35:39.394 21952 21952 E AndroidRuntime: Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatEditText cannot be cast to com.rengwuxian.materialedittext.MaterialEditText
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at com.example.basicaccount.MainActivity.onCreate(MainActivity.java:42)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:8207)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:8191)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3800)
09-19 18:35:39.394 21952 21952 E AndroidRuntime:        ... 11 more

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.basicaccount/com.example.basicaccount.MainActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatEditText cannot be cast to com.rengwuxian.materialedittext.MaterialEditText
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3827)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4003)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatEditText cannot be cast to com.rengwuxian.materialedittext.MaterialEditText
        at com.example.basicaccount.MainActivity.onCreate(MainActivity.java:42)
        at android.app.Activity.performCreate(Activity.java:8207)
        at android.app.Activity.performCreate(Activity.java:8191)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3800)

I'm currently working on a login and sign up app (PHP backend). After dragging the function"logIn()" and the list of edit text and buttons are inside "onCreate" function, the app crashes. On the Logcat, the error indicates that it starts from line 42. After cutting and pasting line 42 to inside the function logIn(), then the error shows from line 41. After repeating the process, the error of the line decreases each time.

In contrary, after cutting from line 42 "mEmail = findViewById(R.id.loginEmail);" to line 60 (closing tag) and pasting into the function logIn(), the app runs, but the buttons do not work because the logIn() function is unused. Basically, the logIn() codes became useless.

I want to make the logIn() function work without crashing the app.

How do I fix this bug?

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private String appURL;
    private String EMAIL, PASSWORD;
    MaterialEditText mEmail, mPassword;
    Button mButton;
    TextView mCreateAccount;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        appURL = "anonymous_link";

        mEmail = findViewById(R.id.loginEmail);
        mPassword = findViewById(R.id.loginPassword);
        mCreateAccount = findViewById(R.id.leadSignup);
        mCreateAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), SignUp.class);
                startActivity(intent);
            }
        });

        mButton = findViewById(R.id.loginBut);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                logIn();
            }
        });
    }





    @SuppressLint("WrongViewCast")
    private void logIn() {



        EMAIL = mEmail.getText().toString();
        PASSWORD = mPassword.getText().toString();

        if (EMAIL.isEmpty()) {
            AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
            alert.setMessage("Please enter your email");
            alert.setCancelable(false);
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            alert.show();
        }
            else if (PASSWORD.isEmpty()) {
            AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
            alert.setMessage("Please enter your password");
            alert.setCancelable(false);
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            alert.show();
        }
        else if (PASSWORD.length() < 10) {
            AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
            alert.setMessage("Password length must not be less than 10 charc!");
            alert.setCancelable(false);
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            alert.show();
        }
        else {
            StringRequest stringRequest;
            stringRequest = new StringRequest(Request.Method.POST, appURL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (response.equals("true")) {
                        Intent intent = new Intent(getApplicationContext(), Dashboard.class);
                        startActivity(intent);
                    } else {
                        AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
                        alert.setMessage(response);
                        alert.setCancelable(false);
                        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                        alert.show();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    AlertDialog.Builder alert, alert1, alert2;
                    NetworkResponse response = error.networkResponse;
                    if (response != null && response.data != null) {
                        switch (response.statusCode) {
                            case 400:
                                alert = new AlertDialog.Builder(getApplicationContext());
                                alert.setTitle("Error");
                                alert.setMessage(response.data.toString());
                                alert.setCancelable(false);
                                alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        dialogInterface.dismiss();
                                    }
                                });
                                alert.show();
                                break;
                            case 404:
                                alert1 = new AlertDialog.Builder(getApplicationContext());
                                alert1.setTitle("Error");
                                alert1.setMessage(response.data.toString());
                                alert1.setCancelable(false);
                                alert1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        dialogInterface.dismiss();
                                    }
                                });
                                alert1.show();
                                break;
                            case 403:
                                alert2 = new AlertDialog.Builder(getApplicationContext());
                                alert2.setTitle("Error");
                                alert2.setMessage(response.data.toString());
                                alert2.setCancelable(false);
                                alert2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        dialogInterface.dismiss();
                                    }
                                });
                                alert2.show();
                                break;
                        }
                    }
                    else {
                        alert = new AlertDialog.Builder(getApplicationContext());
                        alert.setTitle("Error");
                        alert.setMessage("An error occured. Please try again");
                        alert.setCancelable(false);
                        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                        alert.show();
                    }
                }
            })
            {
                @Nullable
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String, String> params = new HashMap<>();
                    params.put("email", EMAIL);
                    params.put("password", PASSWORD);

                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> params = new HashMap<>();
                    params.put("Accept", "Application/json: charset = UTF-8");
                    return super.getHeaders();
                }
            };
            VolleySingleton.getInstance().addRequestQueue(stringRequest);
        }





    }



}
2 Answers

Error is: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatEditText cannot be cast to com.rengwuxian.materialedittext.MaterialEditText

Solution 1 is:

change in MainActivity.java class

MaterialEditText mEmail, mPassword;

to

AppCompatEditText mEmail, mPassword;

Or Solution 2 is...

Chnage ExitText in activity_main.xml

<com.rengwuxian.materialedittext.MaterialEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

To

<androidx.appcompat.widget.AppCompatEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatEditText cannot be cast to com.rengwuxian.materialedittext.MaterialEditText

In this peace of code you are saying that mEmail and mPassword are the type MaterialEditText.

MaterialEditText mEmail, mPassword;

however in the XML layout (R.layout.activity_main) they should be the type of AppCompatEditText, and is that cast that is making your app crash. You should change in the XML layout from AppCompatEditText to MaterialEditText or in your code change to:

AppCompatEditText mEmail, mPassword;
Related