'When I install my app in the phone, it imediately crashes -- Android Studio

Every time I try to run the app, it installs correctly, but then when it tries to open itself, it immediately crashes, and if I try to open it again keeps crashing.

The error that appears is the following:



Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.hearapp.MainActivity" on path: DexPathList[[zip file "/data/app/~~9rWShSVzYzBW9f47F4keiA==/com.example.hearapp-ErMfQhrKVlWTHYu2GasO5w==/base.apk"],nativeLibraryDirectories=[/data/app/~~9rWShSVzYzBW9f47F4keiA==/com.example.hearapp-ErMfQhrKVlWTHYu2GasO5w==/lib/arm64, /system/lib64, /system/system_ext/lib64]]

the main Activity code is this:

package com.example.hearapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.button.MaterialButton;

public class LogIn extends AppCompatActivity {

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

        TextView username = (TextView) findViewById(R.id.Username);
        TextView password = (TextView) findViewById(R.id.Password);

        MaterialButton loginButton = (MaterialButton) findViewById(R.id.LogInButton);

        //both password and username are admin

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(username.getText().toString().equals("admin") && password.getText().equals("admin")) {
                    //correc
                    Toast.makeText(LogIn.this, "Log In Succesfull", Toast.LENGTH_SHORT).show();
                    openMainPage();
                } else {
                    //incorrect
                    Toast.makeText(LogIn.this, "Username or Password Incorrect", Toast.LENGTH_SHORT).show();
                    ImageView imageView = (ImageView) findViewById(R.id.garcias);
                    imageView.setVisibility(View.VISIBLE);

                }
            }
        });

    }
    public void openMainPage(){
        Intent intent = new Intent(this, MainPage.class);
        startActivity(intent);
    }

}
   


Solution 1:[1]

The answer was simple, I had changed the name for the MainActivity class to "SignIn", so when the program tried to open the MainActivity file, it didnt find it since it had another name, So i just had to change the name back to "MainActivity" and problem solved.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Laughcheeta1