尝试在空对象引用上调用虚拟方法'android.widget.EditText android.support.design.widget.TextInputLayout.getEditText()'

问题描述 投票:0回答:1

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("User SignUp");

        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("Please enter all fields");
                else if (!edtPassword.getEditText().getText().toString().equals(edtConfirmPassword.getEditText().getText().toString()))
                    ShowSnackBar("Password does not match");
                else {
                    AddUsers addUsers = new AddUsers();
                    addUsers.execute("");
                }

            }
        });
    }

    public void ShowSnackBar(String message) {
        Snackbar.make(lvparent, message, Snackbar.LENGTH_LONG)
                .setAction("CLOSE", 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 "Added successfully";
            } catch (SQLException e) {
                e.printStackTrace();
                return e.getMessage().toString();
            } catch (Exception e) {
                return "Exception. Please check your code and database.";
            }
        }

        @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("Added successfully")) {
                // 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:textColor="@color/Dark"
                    android:drawableRight="@drawable/ic_email_blue_24dp"
                    android:drawableEnd="@drawable/ic_email_blue_24dp"
                    android:id="@+id/EmailEditText"/>
            </android.support.design.widget.TextInputLayout>   -->


            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_below="@id/pass_input"
                android:id="@+id/pass_input_confirm"
                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/PassEditText1" -->
            </android.support.design.widget.TextInputLayout>
            <ImageButton
                android:id="@+id/pass_view_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_pass_eye_blue_24dp"
                android:layout_alignBottom="@id/pass_input_confirm"
                android:layout_alignRight="@id/pass_input"
                android:layout_alignEnd="@id/pass_input"
                android:layout_marginBottom="15dp"
                />

       <!--    <CheckBox
                android:id="@+id/ageCheckBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/pass_input"
                android:layout_alignStart="@id/pass_input"
                android:textColor="@color/dimGray"
                android:textStyle="bold"
                android:layout_below="@id/pass_input_confirm"
                android:text="@string/age_checkbox"/> -->
        </RelativeLayout>
    </android.support.v7.widget.CardView>
    <Button
        style="@android:style/Widget.Holo.Button.Small"
        android:id="@+id/CreateButton"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/sign_up_layout"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="202dp"
        android:text="@string/sign_up"
        android:textColor="@color/White"
        android:background="@drawable/sign_up_bg"/>

    <TextView
        android:id="@+id/TermsTextView"
        android:layout_below="@id/CreateButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/terms_text_view_text"
        android:textColor="@color/LightGray"
        android:maxLines="2"
        android:layout_centerHorizontal="true"
        android:layout_margin="20dp"
        android:gravity="center"
        />

</RelativeLayout>

`

android android-layout
1个回答
0
投票

删除TextInputLayout并保持EditText视图。问题是因为您使用的是getInputText(),它是TextInputLayout上EditText的子级()。因此,gettign nullpointer异常。

© www.soinside.com 2019 - 2024. All rights reserved.