-2
This a error
 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.loginappaplication, PID: 20994
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.loginappaplication/com.example.loginappaplication.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.loginappaplication.MainActivity> has no zero argument constructor
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        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:2216)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7948)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
     Caused by: java.lang.InstantiationException: java.lang.Class<com.example.loginappaplication.MainActivity> has no zero argument constructor
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1251)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3437)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        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:2216) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:7948) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) 

public class MainActivity extends AppCompatActivity {

private EditText eName;
private EditText ePassword;
private Button eLogin;
private TextView eAttemptsInfo;

private String Username = "Azahracaca";
private String Password = "Caca12345";

boolean isvalid = false;
private int counter = 3;

public MainActivity(Button eLogin) {
    this.eLogin = eLogin;
}


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

    eName = findViewById(R.id.IdName);
    ePassword = findViewById(R.id.etPassword);
    eAttemptsInfo = findViewById(R.id.tvAttemptsInfo);

    eLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String inputName = eName.getText().toString();
            String inputPassword = ePassword.getText().toString();

            if (inputName.isEmpty() || inputPassword.isEmpty())
            {
                Toast.makeText(MainActivity.this, "Pastikan Semuanya Benar !", Toast.LENGTH_SHORT).show();
            }else{

                isvalid = valited(inputName, inputPassword);

                if(!isvalid){
                    counter--;
                    Toast.makeText(MainActivity.this, "Salah Memasukkan Data !", Toast.LENGTH_SHORT).show();

                    eAttemptsInfo.setText("Jumlah Login Tersisa : 3 Kali" + counter);

                    if(counter == 0){
                        eLogin.setEnabled(false);
                    }
                }else {
                    Toast.makeText(MainActivity.this, "Login Sukses !", Toast.LENGTH_SHORT).show(); //ke menu selanjutnya
                    Intent intent = new Intent(MainActivity.this, HomePage.class);
                    startActivity(intent);
                }
            }

        }
    });
}
private boolean valited(String name, String password){
    if(name.equals(Username) && password.equals(Password)){
        return true;
    }
    return  false;
}

}

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841

3 Answers3

0
public MainActivity(Button eLogin) {
    this.eLogin = eLogin;
}

make this constructor empty, and initialize eLogin button using findViewById as you are doing for eName. you should initialize all of your views in/after onCreate(). can not initialize view in class constructor.

Nilesh B
  • 937
  • 1
  • 9
  • 14
0

Why constructor Activity you passing Param?

Edit:

public class MainActivity extends AppcompatActivity

And

Button button = findViewById(R.id....)
LuongXuanNam
  • 67
  • 1
  • 4
0

The structure should like that

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 }
}
anonymous
  • 228
  • 1
  • 11