侦听器导致应用程序Java崩溃

时间:2018-09-13 17:13:34

标签: java android android-studio crash onclicklistener

我的android应用程序中侦听器的相同代码行使其崩溃。我不明白原因。 我必须从LoginActivity获取两个字符串,并在LoginActivityListener中使用它们

这是主要活动:LoginActivity

public class LoginActivity extends Activity{
private static final int REQUEST_READ_CONTACTS = 0;

AutoCompleteTextView email;
EditText password;
Button loginButton;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    LoginActivityListener loginActivityListener = new LoginActivityListener(this);
     /** LOGIN FORM */
    email = (AutoCompleteTextView) findViewById(R.id.email);
    //populateAutoComplete();

    password = (EditText) findViewById(R.id.password);

    loginButton = (Button) findViewById(R.id.login_button);
    loginButton.setOnClickListener(loginActivityListener);

[...]

public AutoCompleteTextView getEmail() {
    return email;
}

public void setEmail(AutoCompleteTextView email) {
    this.email = email;
}

public EditText getPassword() {
    return password;
}

public void setPassword(EditText password) {
    this.password = password;
}

这是监听器活动:LoginActivityListener

public class LoginActivityListener implements View.OnClickListener {
    LoginActivity loginActivity;
    public LoginActivityListener(LoginActivity loginActivity) {
        this.loginActivity = loginActivity;
    }

    AutoCompleteTextView email = loginActivity.getEmail();
    EditText password = loginActivity.getPassword();

   @Override
   public void onClick(View view) {

   }
}

这是日志

09-13 16:53:35.550 7067-7067/it.russo.thenote E/AndroidRuntime: FATAL EXCEPTION: main
Process: it.russo.thenote, PID: 7067
java.lang.RuntimeException: Unable to start activity ComponentInfo{it.russo.thenote/it.russo.thenote.view.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.AutoCompleteTextView it.russo.thenote.view.LoginActivity.getEmail()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.AutoCompleteTextView it.russo.thenote.view.LoginActivity.getEmail()' on a null object reference
    at it.russo.thenote.listener.LoginActivityListener.<init>(LoginActivityListener.java:15)
    at it.russo.thenote.view.LoginActivity.onCreate(LoginActivity.java:32)
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

问题是

AutoCompleteTextView email = loginActivity.getEmail();
EditText password = loginActivity.getPassword();

如果我删除它们,应用程序将运行,但是我需要它们

0 个答案:

没有答案