点击方法崩溃时调用

时间:2018-01-16 07:04:03

标签: android

我不确定为什么我的代码在这里崩溃,我最初认为这是因为我有空的编辑文本,但在实现了一种方法来检查它仍然崩溃。如果将信息放入EditText,它们就能正常运行。任何想法?
这应该是一个登录页面,如果信息有效,则转到BackgroundWorker类以确认数据库。我对应用程序开发场景很陌生,所以非常感谢任何帮助。

public class Login extends AppCompatActivity {
EditText emailText, passText, bookIDText;
public static String email;
AlertDialog alertDialog;

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

    emailText = (EditText) findViewById(R.id.emailTxt);
    passText = (EditText) findViewById(R.id.passTxt);
    bookIDText = (EditText) findViewById(R.id.bookIDTxt);

    Button loginBtn = (Button) findViewById(R.id.LoginBtn);
    TextView welcomeText = (TextView) findViewById(R.id.WelcomeToLibrary);
    TextView signIn = (TextView) findViewById(R.id.SignInBelow);
    TextView clickRegister = (TextView) findViewById(R.id.clickToRegister);

    Typeface openSans = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-CondLight.ttf");

    welcomeText.setTypeface(openSans);
    signIn.setTypeface(openSans);
    clickRegister.setTypeface(openSans);
    loginBtn.setTypeface(openSans);
}

public void onLogin(View view) {
    if (isEmpty(emailText) || isEmpty(passText)) {
        alertDialog.setMessage("Please enter all required fields!");
        alertDialog.show();
    } else {
        email = emailText.getText().toString();
        String password = passText.getText().toString();
        String type = "login";      
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, email, password);
    }
}

public void openRegister(View view) {
    startActivity(new Intent(this, Register.class));
}

private boolean isEmpty(EditText etText) {
    if (etText.getText().toString().trim().length() > 0) {
        return false;
    } else {
        return true;
    }
}
}

这是布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue_background"
tools:context="com.example.hall.pchsmobilelibrary20.Login">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="32dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@drawable/white_rounded_corners"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.31">

    <TextView
        android:id="@+id/WelcomeToLibrary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Welcome to the PCHS Mobile Library!"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/emailTxt"
        android:layout_width="215dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:hint="Student/Staff Email"
        android:inputType="textWebEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/SignInBelow" />

    <EditText
        android:id="@+id/passTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/emailTxt" />

    <TextView
        android:id="@+id/clickToRegister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:clickable="true"
        android:onClick="openRegister"
        android:text="Haven't already registered? Click here!"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/LoginBtn" />

    <Button
        android:id="@+id/LoginBtn"
        android:layout_width="0dp"
        android:layout_height="37dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/blue_rounded_corners"
        android:onClick="onLogin"
        android:text="Login"
        app:layout_constraintEnd_toEndOf="@+id/passTxt"
        app:layout_constraintStart_toStartOf="@+id/passTxt"
        app:layout_constraintTop_toBottomOf="@+id/passTxt" />

    <TextView
        android:id="@+id/SignInBelow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Please Sign In"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/WelcomeToLibrary" />
</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

错误消息

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.hall.pchsmobilelibrary20, PID: 5172
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:6256)
                  at android.view.View$PerformClick.run(View.java:24701)
                  at android.os.Handler.handleCallback(Handler.java:789)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  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.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:6256) 
                  at android.view.View$PerformClick.run(View.java:24701) 
                  at android.os.Handler.handleCallback(Handler.java:789) 
                  at android.os.Handler.dispatchMessage(Handler.java:98) 
                  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 'void android.support.v7.app.AlertDialog.setMessage(java.lang.CharSequence)' on a null object reference
                  at com.example.hall.pchsmobilelibrary20.Login.onLogin(Login.java:45)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:6256) 
                  at android.view.View$PerformClick.run(View.java:24701) 
                  at android.os.Handler.handleCallback(Handler.java:789) 
                  at android.os.Handler.dispatchMessage(Handler.java:98) 
                  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) 

申请已终止。

2 个答案:

答案 0 :(得分:0)

请在onCreate()方法

中添加此行
alertDialog = new AlertDialog.Builder(this).create();

答案 1 :(得分:0)

bookIDText = (EditText) findViewById(R.id.bookIDTxt);

我认为上面的代码会导致错误,因为您的布局中没有EditText个名称bookIDTxt