attempt to invoke virtual method 'android.widget.EditText android.support.design.widget.TextInputLayout.getEditText()' on a null object reference

huangapple go评论65阅读模式
英文:

attempt to invoke virtual method 'android.widget.EditText android.support.design.widget.TextInputLayout.getEditText()' on a null object reference

问题

在Android Studio中出现错误:"java.lang.NullPointerException: 尝试调用虚拟方法 'android.widget.EditText android.support.design.widget.TextInputLayout.getEditText()' 但为空对象引用"。 请帮助我解决这个问题。我已经检查了stackoverflow上的所有解决方案,但无法解决这个问题。

TextInputLayout edtUsername, edtPassword, edtConfirmPassword;
Button btnSignUp;
/* ProgressBar progressBar; */
LinearLayout lvparent;

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

    edtUsername = findViewById(R.id.email_input);
    edtPassword = findViewById(R.id.pass_input);
    edtConfirmPassword = findViewById(R.id.pass_input_confirm);
    btnSignUp = findViewById(R.id.CreateButton);
    /* progressBar = findViewById(R.id.pbbar);
    lvparent = findViewById(R.id.lvparent); */
    this.setTitle("用户注册");

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (isEmpty(edtUsername.getEditText().getText().toString()) ||
            isEmpty(edtPassword.getEditText().getText().toString()) ||
                    isEmpty(edtConfirmPassword.getEditText().getText().toString()))
                ShowSnackBar("请输入所有字段");
            else if (!edtPassword.getEditText().getText().toString().equals(edtConfirmPassword.getEditText().getText().toString()))
                ShowSnackBar("密码不匹配");
            else {
                AddUsers addUsers = new AddUsers();
                addUsers.execute("");
            }

        }
    });
}

public void ShowSnackBar(String message) {
    Snackbar.make(lvparent, message, Snackbar.LENGTH_LONG)
            .setAction("关闭", new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            })
            .setActionTextColor(getResources().getColor(android.R.color.holo_red_light))
            .show();
}

public Boolean isEmpty(String strValue) {
    if (strValue == null || strValue.trim().equals(""))
        return true;
    else
        return false;
}

private class AddUsers extends AsyncTask<String, Void, String> {
    String name, plainPassword;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        name = edtUsername.getEditText().getText().toString();
        plainPassword = edtPassword.getEditText().getText().toString();
        /* progressBar.setVisibility(View.VISIBLE); */
        btnSignUp.setVisibility(View.GONE);
    }

    @Override
    protected String doInBackground(String... params) {

        try {
            ConnectionHelper con = new ConnectionHelper();
            Connection connect = ConnectionHelper.CONN();

            String queryStmt = "Insert into tblUsers " +
                    " (UserId,Password,UserRole) values "
                    + "('" + name + "','" + plainPassword + "','User')";

            PreparedStatement preparedStatement = connect
                    .prepareStatement(queryStmt);

            preparedStatement.executeUpdate();

            preparedStatement.close();

            return "成功添加";
        } catch (SQLException e) {
            e.printStackTrace();
            return e.getMessage().toString();
        } catch (Exception e) {
            return "异常。请检查您的代码和数据库。";
        }
    }

    @Override
    protected void onPostExecute(String result) {

        //Toast.makeText(signup.this, result, Toast.LENGTH_SHORT).show();
        ShowSnackBar(result);
        /* progressBar.setVisibility(View.GONE); */
        btnSignUp.setVisibility(View.VISIBLE);
        if (result.equals("成功添加")) {
            // Clear();
        }

    }
}
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/White">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sign_up_layout"
        android:padding="20dp"
        android:background="@drawable/sign_up_layout_bg">
        <ImageView

            android:id="@+id/logo"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginTop="20dp"
            android:contentDescription="@string/image_view_des"
            android:layout_centerInParent="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/logo" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sign_up"
            android:textColor="@color/White"
            android:typeface="serif"
            android:layout_below="@+id/logo"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="40dp"
            android:textSize="24sp"/>
    </RelativeLayout>
    <android.support.v7.widget.CardView
        android:layout_centerHorizontal="true"
        android:layout_width="350dp"
        android:id="@+id/cardView"
        android:layout_marginTop="200dp"
        android:layout_height="270dp">
        <RelativeLayout
            android:padding="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:id="@+id/full_name_input"
                android:hint="@string/username_hint"
                android:layout_height="wrap_content">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:inputType="text"
                    android:textColor="@color/Dark"
                    android:drawableRight="@drawable/ic_user_blue_24dp"
                    android:drawableEnd="@drawable/ic_user_blue_24dp"
                    android:id="@+id/UserEditText"/>
            </android.support.design.widget.TextInputLayout>
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_below="@id/full_name_input"
                android:id="@+id/pass_input"
                android:hint="@string/password_hint"
                android:layout_height="wrap_content">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:inputType="textPassword"
                    android:textColor="@color/Dark" />
                <!--    android:id="@+id/PassEditText" -->
            </android.support.design.widget.TextInputLayout>
            <ImageButton
                android:id="@+id/pass_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_pass_eye_blue_24dp"
                android:layout_alignBottom="@id/pass_input"
                android:layout_alignRight="@id/pass_input"
                android:layout_alignEnd="@id/pass_input"
                android:layout_marginBottom="15dp"
                />
         <!--   <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_below="@id/pass_input"
                android:id="@+id/email_input"
                android:hint="@string/email_hint"
                android:layout_height="wrap_content">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:inputType="textEmailAddress"
                    android:text

<details>
<summary>英文:</summary>

Am getting error in android studio: &quot;java.lang.NullPointerException: Attempt to invoke virtual method &#39;android.widget.EditText android.support.design.widget.TextInputLayout.getEditText()&#39;&quot; on a null object reference&quot;.   Please help me to resolve this issue. I already check all solution on stackoverflow but could not resolve the issue.
    
 
 `

      TextInputLayout edtUsername, edtPassword, edtConfirmPassword;
        Button btnSignUp;
       /* ProgressBar progressBar; */
        LinearLayout lvparent; 
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_create);
    
            edtUsername = findViewById(R.id.email_input);
            edtPassword = findViewById(R.id.pass_input);
            edtConfirmPassword = findViewById(R.id.pass_input_confirm);
            btnSignUp = findViewById(R.id.CreateButton);
          /*  progressBar = findViewById(R.id.pbbar);
            lvparent = findViewById(R.id.lvparent); */
            this.setTitle(&quot;User SignUp&quot;);
    
            btnSignUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if (isEmpty(edtUsername.getEditText().getText().toString()) ||
                    isEmpty(edtPassword.getEditText().getText().toString()) ||
                            isEmpty(edtConfirmPassword.getEditText().getText().toString()))
                        ShowSnackBar(&quot;Please enter all fields&quot;);
                    else if (!edtPassword.getEditText().getText().toString().equals(edtConfirmPassword.getEditText().getText().toString()))
                        ShowSnackBar(&quot;Password does not match&quot;);
                    else {
                        AddUsers addUsers = new AddUsers();
                        addUsers.execute(&quot;&quot;);
                    }
    
                }
            });
        }
    
        public void ShowSnackBar(String message) {
            Snackbar.make(lvparent, message, Snackbar.LENGTH_LONG)
                    .setAction(&quot;CLOSE&quot;, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
    
                        }
                    })
                    .setActionTextColor(getResources().getColor(android.R.color.holo_red_light))
                    .show();
        }
    
        public Boolean isEmpty(String strValue) {
            if (strValue == null || strValue.trim().equals((&quot;&quot;)))
                return true;
            else
                return false;
        }
    
        private class AddUsers extends AsyncTask&lt;String, Void, String&gt; {
            String name, plainPassword;
    
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
    
                name = edtUsername.getEditText().getText().toString();
                plainPassword = edtPassword.getEditText().getText().toString();
              /*  progressBar.setVisibility(View.VISIBLE); */
                btnSignUp.setVisibility(View.GONE);
            }
    
            @Override
            protected String doInBackground(String... params) {
    
                try {
                    ConnectionHelper con = new ConnectionHelper();
                    Connection connect = ConnectionHelper.CONN();
    
                    String queryStmt = &quot;Insert into tblUsers &quot; +
                            &quot; (UserId,Password,UserRole) values &quot;
                            + &quot;(&#39;&quot;
                            + name
                            + &quot;&#39;,&#39;&quot;
                            + plainPassword
                            + &quot;&#39;,&#39;User&#39;)&quot;;
    
                    PreparedStatement preparedStatement = connect
                            .prepareStatement(queryStmt);
    
                    preparedStatement.executeUpdate();
    
                    preparedStatement.close();
    
                    return &quot;Added successfully&quot;;
                } catch (SQLException e) {
                    e.printStackTrace();
                    return e.getMessage().toString();
                } catch (Exception e) {
                    return &quot;Exception. Please check your code and database.&quot;;
                }
            }
    
            @Override
            protected void onPostExecute(String result) {
    
                //Toast.makeText(signup.this, result, Toast.LENGTH_SHORT).show();
                ShowSnackBar(result);
              /*  progressBar.setVisibility(View.GONE); */
                btnSignUp.setVisibility(View.VISIBLE);
                if (result.equals(&quot;Added successfully&quot;)) {
                    // Clear();
                }
    
            }
        }

`

 

  
`

    &lt;RelativeLayout
        xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:background=&quot;@color/White&quot;&gt;
    
        &lt;RelativeLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:id=&quot;@+id/sign_up_layout&quot;
            android:padding=&quot;20dp&quot;
            android:background=&quot;@drawable/sign_up_layout_bg&quot;&gt;
            &lt;ImageView
    
                android:id=&quot;@+id/logo&quot;
                android:layout_width=&quot;100dp&quot;
                android:layout_height=&quot;100dp&quot;
                android:layout_marginTop=&quot;20dp&quot;
                android:contentDescription=&quot;@string/image_view_des&quot;
                android:layout_centerInParent=&quot;true&quot;
                android:layout_alignParentTop=&quot;true&quot;
                android:src=&quot;@drawable/logo&quot; /&gt;
    
            &lt;TextView
                android:id=&quot;@+id/text&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:text=&quot;@string/sign_up&quot;
                android:textColor=&quot;@color/White&quot;
                android:typeface=&quot;serif&quot;
                android:layout_below=&quot;@+id/logo&quot;
                android:layout_centerHorizontal=&quot;true&quot;
                android:layout_marginTop=&quot;10dp&quot;
                android:layout_marginBottom=&quot;40dp&quot;
                android:textSize=&quot;24sp&quot;/&gt;
        &lt;/RelativeLayout&gt;
        &lt;android.support.v7.widget.CardView
            android:layout_centerHorizontal=&quot;true&quot;
            android:layout_width=&quot;350dp&quot;
            android:id=&quot;@+id/cardView&quot;
            android:layout_marginTop=&quot;200dp&quot;
            android:layout_height=&quot;270dp&quot;&gt;
            &lt;RelativeLayout
                android:padding=&quot;10dp&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;&gt;
                &lt;android.support.design.widget.TextInputLayout
                    android:layout_width=&quot;match_parent&quot;
                    android:id=&quot;@+id/full_name_input&quot;
                    android:hint=&quot;@string/username_hint&quot;
                    android:layout_height=&quot;wrap_content&quot;&gt;
                    &lt;EditText
                        android:layout_width=&quot;match_parent&quot;
                        android:layout_height=&quot;50dp&quot;
                        android:inputType=&quot;text&quot;
                        android:textColor=&quot;@color/Dark&quot;
                        android:drawableRight=&quot;@drawable/ic_user_blue_24dp&quot;
                        android:drawableEnd=&quot;@drawable/ic_user_blue_24dp&quot;
                        android:id=&quot;@+id/UserEditText&quot;/&gt;
                &lt;/android.support.design.widget.TextInputLayout&gt;
                &lt;android.support.design.widget.TextInputLayout
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_below=&quot;@id/full_name_input&quot;
                    android:id=&quot;@+id/pass_input&quot;
                    android:hint=&quot;@string/password_hint&quot;
                    android:layout_height=&quot;wrap_content&quot;&gt;
                    &lt;EditText
                        android:layout_width=&quot;match_parent&quot;
                        android:layout_height=&quot;50dp&quot;
                        android:inputType=&quot;textPassword&quot;
                        android:textColor=&quot;@color/Dark&quot; /&gt;
                    &lt;!--    android:id=&quot;@+id/PassEditText&quot; --&gt;
                &lt;/android.support.design.widget.TextInputLayout&gt;
                &lt;ImageButton
                    android:id=&quot;@+id/pass_view&quot;
                    android:layout_width=&quot;wrap_content&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:background=&quot;@drawable/ic_pass_eye_blue_24dp&quot;
                    android:layout_alignBottom=&quot;@id/pass_input&quot;
                    android:layout_alignRight=&quot;@id/pass_input&quot;
                    android:layout_alignEnd=&quot;@id/pass_input&quot;
                    android:layout_marginBottom=&quot;15dp&quot;
                    /&gt;
             &lt;!--   &lt;android.support.design.widget.TextInputLayout
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_below=&quot;@id/pass_input&quot;
                    android:id=&quot;@+id/email_input&quot;
                    android:hint=&quot;@string/email_hint&quot;
                    android:layout_height=&quot;wrap_content&quot;&gt;
                    &lt;EditText
                        android:layout_width=&quot;match_parent&quot;
                        android:layout_height=&quot;50dp&quot;
                        android:inputType=&quot;textEmailAddress&quot;
                        android:textColor=&quot;@color/Dark&quot;
                        android:drawableRight=&quot;@drawable/ic_email_blue_24dp&quot;
                        android:drawableEnd=&quot;@drawable/ic_email_blue_24dp&quot;
                        android:id=&quot;@+id/EmailEditText&quot;/&gt;
                &lt;/android.support.design.widget.TextInputLayout&gt;   --&gt;
    
    
                &lt;android.support.design.widget.TextInputLayout
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_below=&quot;@id/pass_input&quot;
                    android:id=&quot;@+id/pass_input_confirm&quot;
                    android:hint=&quot;@string/password_hint&quot;
                    android:layout_height=&quot;wrap_content&quot;&gt;
                    &lt;EditText
                        android:layout_width=&quot;match_parent&quot;
                        android:layout_height=&quot;50dp&quot;
                        android:inputType=&quot;textPassword&quot;
                        android:textColor=&quot;@color/Dark&quot; /&gt;
                      &lt;!--  android:id=&quot;@+id/PassEditText1&quot; --&gt;
                &lt;/android.support.design.widget.TextInputLayout&gt;
                &lt;ImageButton
                    android:id=&quot;@+id/pass_view_1&quot;
                    android:layout_width=&quot;wrap_content&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:background=&quot;@drawable/ic_pass_eye_blue_24dp&quot;
                    android:layout_alignBottom=&quot;@id/pass_input_confirm&quot;
                    android:layout_alignRight=&quot;@id/pass_input&quot;
                    android:layout_alignEnd=&quot;@id/pass_input&quot;
                    android:layout_marginBottom=&quot;15dp&quot;
                    /&gt;
    
           &lt;!--    &lt;CheckBox
                    android:id=&quot;@+id/ageCheckBox&quot;
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:layout_alignLeft=&quot;@id/pass_input&quot;
                    android:layout_alignStart=&quot;@id/pass_input&quot;
                    android:textColor=&quot;@color/dimGray&quot;
                    android:textStyle=&quot;bold&quot;
                    android:layout_below=&quot;@id/pass_input_confirm&quot;
                    android:text=&quot;@string/age_checkbox&quot;/&gt; --&gt;
            &lt;/RelativeLayout&gt;
        &lt;/android.support.v7.widget.CardView&gt;
        &lt;Button
            style=&quot;@android:style/Widget.Holo.Button.Small&quot;
            android:id=&quot;@+id/CreateButton&quot;
            android:layout_width=&quot;250dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_below=&quot;@id/sign_up_layout&quot;
            android:layout_centerHorizontal=&quot;true&quot;
            android:layout_marginTop=&quot;202dp&quot;
            android:text=&quot;@string/sign_up&quot;
            android:textColor=&quot;@color/White&quot;
            android:background=&quot;@drawable/sign_up_bg&quot;/&gt;
    
        &lt;TextView
            android:id=&quot;@+id/TermsTextView&quot;
            android:layout_below=&quot;@id/CreateButton&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;@string/terms_text_view_text&quot;
            android:textColor=&quot;@color/LightGray&quot;
            android:maxLines=&quot;2&quot;
            android:layout_centerHorizontal=&quot;true&quot;
            android:layout_margin=&quot;20dp&quot;
            android:gravity=&quot;center&quot;
            /&gt;
    
    &lt;/RelativeLayout&gt;

 `


</details>


# 答案1
**得分**: 0

只保留`EditText`视图,移除`TextInputLayout`。问题出在您在`TextInputLayout`上使用`getEditText()`,它是`EditText`的`child()`,因此导致了空指针异常。

<details>
<summary>英文:</summary>

Remove `TextInputLayout` and just keep `EditText` view. The problem is because you are using the `getEditText()` which is a `child()` of `EditText` on `TextInputLayout`. Hence, gettign nullpointer exception.

</details>



# 答案2
**得分**: 0

将这些id设置为`EditText`,而不是`TextInputLayout`,并相应地更改您的代码如下所示:

```java
EditText edtUsername, edtPassword, edtConfirmPassword;

@Override
public void onClick(View v) {

    if (isEmpty(edtUsername.getText().toString()) ||
            isEmpty(edtPassword.getText().toString()) ||
            isEmpty(edtConfirmPassword.getText().toString()))
        ShowSnackBar("请输入所有字段");
    else if (!edtPassword.getText().toString().equals(edtConfirmPassword.getText().toString()))
        ShowSnackBar("密码不匹配");
    else {
        AddUsers addUsers = new AddUsers();
        addUsers.execute("");
    }
}

请注意,我已将双引号内的文本从HTML实体(&quot;)转换为普通文本。

英文:

Set those id email_input , pass_input and pass_input_confirm to EditText instead of TextInputLayout and change your code accordingly like below:

EditText edtUsername, edtPassword, edtConfirmPassword;
@Override
public void onClick(View v) {
if (isEmpty(edtUsername.getText().toString()) ||
isEmpty(edtPassword.getText().toString()) ||
isEmpty(edtConfirmPassword.getText().toString()))
ShowSnackBar(&quot;Please enter all fields&quot;);
else if (!edtPassword.getText().toString().equals(edtConfirmPassword.getText().toString()))
ShowSnackBar(&quot;Password does not match&quot;);
else {
AddUsers addUsers = new AddUsers();
addUsers.execute(&quot;&quot;);
}
}

huangapple
  • 本文由 发表于 2020年1月7日 02:21:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定