首页 > 解决方案 > 在布局文件的 Imageview 中获取空指针异常

问题描述

您好,我的问题是我在我的程序中遇到空指针异常我在布局文件的图像视图中遇到异常我正在尝试制作一个 firebase 应用程序所以我使用 try-catch 块来处理 firebase 错误所以我决定制作一个漂亮的对话框但是当我第一次测试它时它运行但现在它抛出一个空指针异常所以请帮助我这是我的代码和活动

:- 我的注册活动

public class registerActivity extends Activity {
Dialog error_dialog_box_register;
    TextView dialog_title_register, dialog_message_register;
    ImageView dialog_main_image, dialog_cancel_image;
    Button ok_button, login_button;
    private Button RegisterButton;

@Override
    protected void onCreate(Bundle savedInstanceState) {
RegisterButton = findViewById(R.id.register_in_button);
RegisterButtonListener();
}
private void RegisterButtonListener() {
        RegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CreateNewAccount();

            }
        });
    }

 private void CreateNewAccount() {
mAuth.createUserWithEmailAndPassword(Email, Password)
                    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {

                            if (task.isSuccessful()) {
                                Register_Progress_bar.setVisibility(View.GONE);
                                SendUserToDate_of_birthActivity();
                            } else {
                                try {
                                    Register_Progress_bar.setVisibility(View.GONE);
                                    throw Objects.requireNonNull(task.getException());
                                } catch (FirebaseAuthUserCollisionException f) {
                                    ShowErrorPopup();
                                    Toast.makeText(registerActivity.this, "User already exists", Toast.LENGTH_SHORT).show();
                                } catch (FirebaseNetworkException e) {
                                    Toast.makeText(registerActivity.this, "Error network error", Toast.LENGTH_SHORT).show();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

                        }
                    });
}
    @SuppressLint("SetTextI18n")
    private void ShowErrorPopup() {


        error_dialog_box_register.setContentView(R.layout.custom_popup_p);

        dialog_cancel_image = (ImageView) findViewById(R.id.Cancel_dialog_box_register);
        ok_button = (Button) findViewById(R.id.dialog_ok_button_register);
        login_button = (Button) findViewById(R.id.dialog_login_button_register);
        dialog_title_register = (TextView) findViewById(R.id.Dialog_title_register);
        dialog_message_register = (TextView) findViewById(R.id.dialog_message_register);

        dialog_cancel_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                error_dialog_box_register.dismiss();
            }
        });

        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SendUserToLoginActivity();
            }
        });

        dialog_title_register.setText("Error");
        dialog_message_register.setText("Email already exists please login or try again");

        error_dialog_box_register.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        error_dialog_box_register.show();




    }

}

这是我的 xml 文件:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/Cancel_dialog_box_register"
        android:layout_width="46dp"
        android:layout_height="38dp"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="7dp"
        android:layout_marginEnd="7dp"
        android:elevation="5dp"
        android:src="@drawable/ic_close_black_24dp"
        />


    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        app:cardBackgroundColor="@color/ColorRed"
        app:cardCornerRadius="15dp"

        >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                android:layout_marginBottom="25dp"
                android:orientation="vertical">


                <TextView
                    android:id="@+id/Dialog_title_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:text=""
                    android:textAlignment="center"
                    android:textColor="#000000"
                    android:textSize="25sp"
                    android:textStyle="bold" />

                <ImageView
                    android:id="@+id/dialog_main_image_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginBottom="10dp"
                    android:src="@drawable/ic_error_black_24dp" />

                <TextView
                    android:id="@+id/dialog_message_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginBottom="20dp"
                    android:text=""
                    android:textAlignment="center"
                    android:textColor="#000000"
                    android:textSize="18sp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/dialog_login_button_register"
                        android:layout_width="180dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="start"
                        android:layout_marginStart="20dp"
                        android:layout_marginTop="20dp"
                        android:background="@drawable/login_b_custom"
                        android:text="@string/login"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="15sp" />

                    <Button
                        android:id="@+id/dialog_ok_button_register"
                        android:layout_width="180dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="end"
                        android:layout_marginStart="15dp"
                        android:layout_marginTop="20dp"
                        android:background="@drawable/login_b_custom"
                        android:text="Ok"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="15sp"

                        />
                </LinearLayout>


            </LinearLayout>


        </LinearLayout>


    </androidx.cardview.widget.CardView>


</RelativeLayout>

我收到了这样的错误

2019-08-03 15:52:13.840 5711-5711/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2019-08-03 15:52:13.841 5711-5711/? E/Zygote: accessInfo : 1
2019-08-03 15:52:43.642 5711-5711/com.geetmp3.themusicapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.geetmp3.themusicapp, PID: 5711
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.geetmp3.themusicapp.AccountCreation.registerActivity.ShowErrorPopup(registerActivity.java:193)
        at com.geetmp3.themusicapp.AccountCreation.registerActivity.access$300(registerActivity.java:34)
        at com.geetmp3.themusicapp.AccountCreation.registerActivity$2.onComplete(registerActivity.java:146)
        at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7076)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

请回复我为什么我得到空指针异常,请帮助我

标签: javaandroid

解决方案


在您的OnCreate功能中,您分配RegisterButton为:

RegisterButton = findViewById(R.id.register_in_button);

但在您的布局中,您有两个按钮,其 ID 为:

android:id="@+id/dialog_login_button_register"

android:id="@+id/dialog_ok_button_register"

因此,您的findViewById调用返回 null,因为不register_in_button存在 ID 为的按钮。然后,当您尝试调用空对象的方法时,您会得到一个异常。


推荐阅读