在布局文件的ImageView中获取NullPointer异常



你好,我的问题是我在程序中得到了无效的指针例外,我在一个布局文件的imageview中得到了一个例外,我正在尝试制作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)

请回答我为什么我会得到无效的指针异常,请帮助我

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

RegisterButton = findViewById(R.id.register_in_button);

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

android:id="@+id/dialog_login_button_register"

android:id="@+id/dialog_ok_button_register"

因此,您的 findViewById呼叫以null返回,因为存在具有register_in_button ID的按钮。然后,当您尝试调用Null对象的方法时,您会得到异常。

请通过对话框创建变量参考,因为所有imageveiw或textView都在对话框内部

    error_dialog_box_register= new Dialog(this);
    error_dialog_box_register.requestWindowFeature(Window.FEATURE_NO_TITLE);
    error_dialog_box_register.setContentView(R.layout.custom_popup_p);
    error_dialog_box_register.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    error_dialog_box_register.setCancelable(false);
    dialog_cancel_image = error_dialog_box_register.findViewById(R.id.Cancel_dialog_box_register);

最新更新