0

I'm trying to findViewById in constructor in Login class but it throws a error on object create and crash application.

Error throws only on construct in Login class not on listener. After button clicked it should call loginAction().

What should i do to fix that (this is my first app) ?

Error:

11-26 09:13:57.592 5926-5926/com.wolfriders E/AndroidRuntime: FATAL EXCEPTION: main
                                                          Process: com.wolfriders, PID: 5926
                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wolfriders/com.wolfriders.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                              at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                              at android.os.Handler.dispatchMessage(Handler.java:105)
                                                              at android.os.Looper.loop(Looper.java:164)
                                                              at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                              at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                                                              at android.app.Activity.findViewById(Activity.java:2563)
                                                              at com.wolfriders.Login.<init>(Login.java:13)
                                                              at com.wolfriders.Main.onCreate(Main.java:18)
                                                              at android.app.Activity.performCreate(Activity.java:6975)
                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                              at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                              at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                              at android.os.Looper.loop(Looper.java:164) 
                                                              at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                              at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

Main:

package com.wolfriders;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class Main extends AppCompatActivity {

private boolean logged = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (logged == false) {
        setContentView(R.layout.login_from);

        final Login login = new Login();

        login.login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login.loginAction();
            }

        });
    } else { setContentView(R.layout.main_activity); }
}
}

Login:

package com.wolfriders;

import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Activity {

public EditText login_phone, login_password;
public Button login_btn;

Login() {
    this.login_phone = (EditText)findViewById(R.id.inputloginpassword);
    this.login_password = (EditText)findViewById(R.id.inputloginpassword);
    this.login_btn = (Button)findViewById(R.id.btnlogin);
}

public void loginAction() {
    // Do something after login button clicked
}

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Skyter
  • 13
  • 1
  • what you have to tried? why you are not use findViewById in Main Class ? why seprate class for login ? what make it sense ?? – Munir Nov 26 '17 at 09:22
  • I want to create login form with validation which will be performed in Login class but i want to have variables EditText, Button in Login class not in Main but when i do that it throws error. I tried to set it in Main class and every thing work properly. – Skyter Nov 26 '17 at 09:30

1 Answers1

0

If your Login Class needs to be an Activity you would need to override onCreate function since it is activity and then you need an .xml file to associate that activity class with ui represented. How did you make this Login class (I mean as an activity or just a class)? On more detailed reading and explanation about activities you can read here:

https://developer.android.com/guide/components/activities/intro-activities.html

nikjov92
  • 112
  • 9