java - 在布局文件的 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)
请回复我为什么我得到空指针异常,请帮助我
解决方案
在您的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 为的按钮。然后,当您尝试调用空对象的方法时,您会得到一个异常。
推荐阅读
- shiny - R Shiny Reactive 值,dplyr 过滤器错误?
- bitbucket - 最近的 bitbucket TLS 更新问题(webhook)
- asp.net-core - 如何将 Telerik Reporting 集成到 ASP.NET 零模板中?
- javascript - 来自 Textinput 表单提交的 Next.js 浅路由
- android - 使用braintree库时Android资源链接失败
- oracle - 如何在 SQL 中使用 MIN 和 MAX 函数避免 0 值?
- python - 如何增量训练 H2O 朴素贝叶斯分类器?
- http - 我应该使用什么 HTTP 动词来比较数据
- python-3.x - 防止 BeautifulSoup 的 find_all() 转换转义的 html 标签
- assembly - 使用 int16h 读取 shift+arrow