空指针异常:尝试调用虚拟方法

时间:2019-03-17 08:51:47

标签: java android nullpointerexception android-edittext

我正在尝试为应用开发注册页面。但是我遇到了这个错误。

java.lang.NullPointerException:尝试在null对象引用上调用虚拟方法'android.text.Editable android.widget.EditText.getText()'。我知道这是什么意思,但我已经看过代码和还找到了一些有关Null Pointer Exception的帖子,但看起来还不错。有人可以查看我的代码吗?我想念的是这里的代码。 另外,当我单击错误时,会出现该行代码 if(dataSnapshot.child(edtPhone.getText()。toString())。exists())

所以我想问题就在那儿

公共类SignUp扩展了AppCompatActivity {

EditText edtPhone,edtName,edtPassword;
Button btnSignUp;

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

    edtName = (EditText) findViewById(R.id.edtName);
    edtPassword = (EditText)findViewById(R.id.edtPassword);
   edtPhone = (EditText)findViewById(R.id.edtPhone);
    btnSignUp = (Button)findViewById(R.id.btnSignUp);

    //Init Firebase
     final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference table_user = database.getReference("User");

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final ProgressDialog mDialog = new ProgressDialog(SignUp.this);
            mDialog.setMessage("Please waiting ...");
            mDialog.show();

            table_user.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //check if already user Phone
                    if(dataSnapshot.child(edtPhone.getText().toString()).exists())
                    {
                        mDialog.dismiss();
                        Toast.makeText(SignUp.this, "Phone Number already registered", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        mDialog.dismiss();
                        User user = new User((edtName.getText().toString()),edtPassword.getText().toString());
                        table_user.child(edtPhone.getText().toString()).setValue(user);
                        Toast.makeText(SignUp.this, "Sign Up successful", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });
}

}

这是主要活动注册

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/coffee_pot_backround"

tools:context=".SignUp">

<EditText
    android:id="@+id/edtPhone"

    android:layout_width="276dp"

    android:layout_height="wrap_content"

    android:layout_marginStart="16dp"

    android:layout_marginLeft="16dp"

    android:layout_marginTop="132dp"

    android:layout_marginEnd="119dp"

    android:layout_marginRight="119dp"

    android:ems="10"

    android:hint="Phone Number"

    android:inputType="phone"

    android:textColor="@color/common_google_signin_btn_text_dark_focused"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toTopOf="parent" />


<EditText

    android:id="@+id/edtName"

    android:layout_width="272dp"

    android:layout_height="wrap_content"

    android:layout_marginStart="16dp"

    android:layout_marginLeft="16dp"

    android:layout_marginTop="59dp"

    android:layout_marginEnd="123dp"

    android:layout_marginRight="123dp"

    android:ems="10"

    android:hint="User Name"

    android:inputType="textPersonName"

    android:textColor="@color/common_google_signin_btn_text_dark_focused"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toBottomOf="@+id/edtPhone" />

<EditText
    android:id="@+id/edtPassword"

    android:layout_width="272dp"

    android:layout_height="wrap_content"

    android:layout_marginStart="16dp"

    android:layout_marginLeft="16dp"

    android:layout_marginTop="61dp"

    android:layout_marginEnd="123dp"

    android:layout_marginRight="123dp"

    android:ems="10"

    android:hint="Password"

    android:inputType="textPassword"

    android:textColor="@color/common_google_signin_btn_text_dark_default"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toBottomOf="@+id/edtName" />

<Button

    android:id="@+id/btnSignUp"

    style="@style/Widget.AppCompat.Button.Colored"

    android:layout_width="355dp"

    android:layout_height="wrap_content"

    android:layout_marginStart="26dp"

    android:layout_marginLeft="26dp"

    android:layout_marginTop="248dp"

    android:layout_marginEnd="30dp"

    android:layout_marginRight="30dp"

    android:text="Sign Up"

    android:textSize="18sp"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintHorizontal_bias="1.0"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toBottomOf="@+id/edtPassword" />

任何帮助将不胜感激

0 个答案:

没有答案