You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中调用MaterialEditText.getText()出现空对象引用异常排查

解决“Attempt to invoke virtual method `MaterialEditText.getText()' on a null object reference”异常

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:

  1. In MainActivity.onCreate(), you're trying to initialize edtUser and edtPassword using findViewById(R.id.edtUser) and findViewById(R.id.edtPassword).
  2. But the activity_main.xml you provided doesn't include these two input fields at all! It only has a single MaterialEditText with ID edtNewUserName, plus no sign-in/sign-up buttons. On top of that, the MaterialEditText tag 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>

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:42:27