Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修复EditInputText空对象错误?_Java_Android_Android Textinputedittext - Fatal编程技术网

Java 如何修复EditInputText空对象错误?

Java 如何修复EditInputText空对象错误?,java,android,android-textinputedittext,Java,Android,Android Textinputedittext,我的Android应用程序出现了问题。我创建了一个函数,用于在单击按钮后从SQLite验证我的登录凭据,但它显示了null对象的错误。我已经初始化了我的textInputLayout和TextInputItemText以及我的按钮!!有什么想法吗?谢谢 错误 My LoginActivity.java: package com.example.myapplication; import android.content.Intent; import android.os.Bundle; impo

我的Android应用程序出现了问题。我创建了一个函数,用于在单击按钮后从SQLite验证我的登录凭据,但它显示了null对象的错误。我已经初始化了我的textInputLayout和TextInputItemText以及我的按钮!!有什么想法吗?谢谢

错误

My 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>

您在布局和活动中的Id错误(textInputLayoutEmail、TextInputItemTextMail)

使用正确的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);

检查layout(loginpage)和LoginActivity中的ID是否完全相同。我认为第一个错误是关于VerifyFromSQLite函数的。但我看不出原因。您的
textinputtextUserName
textInputLayoutUsername
为空,但我看到您已初始化它们,这意味着您可能用错误的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);