Android中调用MaterialEditText.getText()出现空对象引用异常排查
Alright, let's break down exactly what's causing this frustrating null pointer exception and how to fix it step by step.
核心原因
This error pops up because when you tap the sign-in button, your code tries to call getText() on either edtUser or edtPassword—but one (or both) of those MaterialEditText references is null.
Looking at your code and layout files:
- In
MainActivity.onCreate(), you're trying to initializeedtUserandedtPasswordusingfindViewById(R.id.edtUser)andfindViewById(R.id.edtPassword). - But the
activity_main.xmlyou provided doesn't include these two input fields at all! It only has a singleMaterialEditTextwith IDedtNewUserName, plus no sign-in/sign-up buttons. On top of that, theMaterialEditTexttag in your layout isn't properly closed, which breaks layout loading entirely.
修复步骤
1. Fix your activity_main.xml layout
First, update your main layout to include all the login UI elements you're referencing in code, and fix the XML syntax error. Here's a corrected version:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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="@color/colorPrimary" tools:context=".MainActivity"> <RelativeLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" > <android.support.v7.widget.CardView android:id="@+id/info_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" app:cardElevation="4dp" > <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp"> <!-- 登录用户名输入框 --> <com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edtUser" android:hint="登录用户名" android:textColorHint="@color/colorPrimary" android:textColor="@color/colorPrimary" android:textSize="24sp" android:layout_width="match_parent" android:layout_height="wrap_content" app:met_baseColor="@color/colorPrimary"/> <!-- 登录密码输入框 --> <com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edtPassword" android:hint="登录密码" android:textColorHint="@color/colorPrimary" android:textColor="@color/colorPrimary" android:textSize="24sp" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" app:met_baseColor="@color/colorPrimary"/> <!-- 登录/注册按钮 --> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:gravity="end"> <Button android:id="@+id/btn_sign_in" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:backgroundTint="@color/colorPrimary" android:textColor="@android:color/white"/> <Button android:id="@+id/btn_sign_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:backgroundTint="@color/colorPrimary" android:textColor="@android:color/white" android:layout_marginLeft="16dp"/> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> </RelativeLayout> </android.support.constraint.ConstraintLayout>
2. Verify your sign_up_layout.xml
Make sure your registration dialog layout (sign_up_layout.xml) includes all the fields you're referencing in showSignUpDialog(). Here's a working example:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp"> <com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edtNewUserName" android:hint="新用户名" android:textColorHint="@color/colorPrimary" android:textColor="@color/colorPrimary" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content" app:met_baseColor="@color/colorPrimary"/> <com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edtNewEmail" android:hint="邮箱" android:textColorHint="@color/colorPrimary" android:textColor="@color/colorPrimary" android:textSize="20sp" android:inputType="textEmailAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" app:met_baseColor="@color/colorPrimary"/> <com.rengwuxian.materialedittext.MaterialEditText android:id="@+id/edtNewPassword" android:hint="新密码" android:textColorHint="@color/colorPrimary" android:textColor="@color/colorPrimary" android:textSize="20sp" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" app:met_baseColor="@color/colorPrimary"/> </LinearLayout>
3. Add defensive checks (optional but recommended)
To avoid similar issues in the future, add quick null checks before accessing your UI elements. For example, update your sign-in click listener:
btnSignIn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 检查控件是否正常加载 if (edtUser == null || edtPassword == null) { Toast.makeText(MainActivity.this, "UI加载失败,请检查布局文件!", Toast.LENGTH_SHORT).show(); return; } String user = edtUser.getText().toString().trim(); String pwd = edtPassword.getText().toString().trim(); signIn(user, pwd); } });
Once you apply these fixes, the null pointer exception should disappear, and your login/registration flow should work as expected.
内容的提问来源于stack exchange,提问作者Cristina Gliga

