我在Android应用程序上遇到问题。我创建了一个函数来单击按钮后从SQLite验证我的登录凭据,但是它向我显示了null对象的错误。我已经初始化了我的textInputLayout和textInputEditText以及我的按钮!有任何想法吗?谢谢!!
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 19517
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.support.design.widget.TextInputEditText.getText()' on a null object reference
at com.example.myapplication.InputValidation.isInputEditTextFilled(InputValidation.java:32)
at com.example.myapplication.LoginActivity.verifyFromSQLite(LoginActivity.java:92)
at com.example.myapplication.LoginActivity.onClick(LoginActivity.java:81)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I/zygote: Background concurrent copying GC freed 10932(1663KB) AllocSpace objects, 1(20KB) LOS objects, 50% free, 1726KB/3MB, paused 11.887ms total 60.469ms
我的LoginActivity.java:
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.view.View;
import com.example.myapplication.R;
import com.example.myapplication.InputValidation;
import com.example.myapplication.DatabaseHelper;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
private final AppCompatActivity activity=LoginActivity.this;
private NestedScrollView nestedScrollView;
private TextInputLayout textInputLayoutUsername;
private TextInputLayout textInputLayoutPassword;
private TextInputEditText textInputEditTextUsername;
private TextInputEditText textInputEditTextPassword;
private AppCompatButton appCompatButtonLogin;
private AppCompatTextView textViewLinkRegister;
private InputValidation inputValidation;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
getSupportActionBar().hide();
initViews();
initListeners();
initObjects();
}
private void initViews(){
nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);
textInputLayoutUsername = (TextInputLayout) findViewById(R.id.textInputLayoutEmail);
textInputLayoutPassword = (TextInputLayout) findViewById(R.id.textInputLayoutPassword);
textInputEditTextUsername = (TextInputEditText) findViewById(R.id.textInputEditTextEmail);
textInputEditTextPassword = (TextInputEditText) findViewById(R.id.textInputEditTextPassword);
appCompatButtonLogin = (AppCompatButton) findViewById(R.id.appCompatButtonLogin);
textViewLinkRegister = (AppCompatTextView) findViewById(R.id.textViewLinkRegister);
}
private void initListeners() {
appCompatButtonLogin.setOnClickListener(this);
textViewLinkRegister.setOnClickListener(this);
}
private void initObjects() {
databaseHelper = new DatabaseHelper(activity);
inputValidation = new InputValidation(activity);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.appCompatButtonLogin:
verifyFromSQLite();
break;
case R.id.textViewLinkRegister:
// Navigate to Register
Intent intentRegister = new Intent(getApplicationContext(), Register.class);
startActivity(intentRegister);
break;
}
}
private void verifyFromSQLite() {
if(!inputValidation.isInputEditTextFilled( textInputEditTextUsername,textInputLayoutUsername,getString( R.string.error_message_username ) )){
return;
}
if(!inputValidation.isInputEditTextFilled( textInputEditTextPassword,textInputLayoutPassword,getString( R.string.error_message_password_match ) )){
return;
}
if (databaseHelper.checkUser( textInputEditTextUsername.getText().toString().trim(),textInputEditTextPassword.getText().toString().trim() )){
Intent accountIntent=new Intent(activity,UsersListActivity.class);
accountIntent.putExtra( "USERNAME",textInputEditTextUsername.getText().toString().trim() );
emptyInputEditText();
startActivity( accountIntent );
}else{
Snackbar.make( nestedScrollView,getString( R.string.error_valid_username_password ),Snackbar.LENGTH_LONG ).show();
}
}
private void emptyInputEditText() {
textInputEditTextUsername.setText( null );
textInputEditTextPassword.setText( null );
}
}
我的InputValidation.java
package com.example.myapplication;
import android.app.Activity;
import android.content.Context;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
public class InputValidation {
private Context context;
/**
* constructor
*
* @param context
*/
public InputValidation(Context context) {
this.context = context;
}
/**
* method to check InputEditText filled .
*
* @param textInputEditText
* @param textInputLayout
* @param message
* @return
*/
public boolean isInputEditTextFilled(TextInputEditText textInputEditText, TextInputLayout textInputLayout, String message) {
String value = textInputEditText.getText().toString().trim();
if (value.isEmpty()) {
textInputLayout.setError(message);
hideKeyboardFrom(textInputEditText);
return false;
} else {
textInputLayout.setErrorEnabled(false);
}
return true;
}
/**
* method to check InputEditText has valid email .
*
* @param textInputEditText
* @param textInputLayout
* @param message
* @return
*/
public boolean isInputEditTextEmail(TextInputEditText textInputEditText, TextInputLayout textInputLayout, String message) {
String value = textInputEditText.getText().toString().trim();
if (value.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(value).matches()) {
textInputLayout.setError(message);
hideKeyboardFrom(textInputEditText);
return false;
} else {
textInputLayout.setErrorEnabled(false);
}
return true;
}
public boolean isInputEditTextMatches(TextInputEditText textInputEditText1, TextInputEditText textInputEditText2, TextInputLayout textInputLayout, String message) {
String value1 = textInputEditText1.getText().toString().trim();
String value2 = textInputEditText2.getText().toString().trim();
if (!value1.contentEquals(value2)) {
textInputLayout.setError(message);
hideKeyboardFrom(textInputEditText2);
return false;
} else {
textInputLayout.setErrorEnabled(false);
}
return true;
}
/**
* method to Hide keyboard
*
* @param view
*/
private void hideKeyboardFrom(View view) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
布局:-
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
tools:context=".LoginActivity">
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
/>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_username"
android:inputType="text"
android:maxLines="1"
android:textColor="@android:color/white" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInputEditTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="@android:color/white" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/appCompatButtonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="@color/colorTextHint"
android:text="@string/text_login" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textViewLinkRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="@string/text_not_member"
android:textSize="16dp" />
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v4.widget.NestedScrollView>
答案 0 :(得分:1)
您在布局和活动中的ID错误(textInputLayoutEmail,textInputEditTextEmail)
使用正确的ID
textInputLayoutUsername = (TextInputLayout) findViewById(R.id.textInputLayoutUsername);
textInputLayoutPassword = (TextInputLayout) findViewById(R.id.textInputLayoutPassword);
textInputEditTextUsername = (TextInputEditText) findViewById(R.id.textInputEditTextUsername);
textInputEditTextPassword = (TextInputEditText) findViewById(R.id.textInputEditTextPassword);