gpt4 book ai didi

java - 按钮在 Android 警报 View 中不起作用?

转载 作者:行者123 更新时间:2023-12-01 20:01:51 25 4
gpt4 key购买 nike

我创建了一个客户对话框,在该对话框中我声明了两个用于图像上传的按钮,但如果由于以下错误我无法转到此对话框

Process: com.example.crowderia.eatitserver, PID: 23218 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.crowderia.eatitserver.HomeActivity.showDialog(HomeActivity.java:135) at com.example.crowderia.eatitserver.HomeActivity.access$000(HomeActivity.java:46) at com.example.crowderia.eatitserver.HomeActivity$1.onClick(HomeActivity.java:87) at android.view.View.performClick(View.java:4848) at android.view.View$PerformClick.run(View.java:20300) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:210) at android.app.ActivityThread.main(ActivityThread.java:5839) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)

如果我删除按钮的 setOnclick 监听器,它会显示对话框我该如何解决这个问题,请帮助我,我是新人

private void showDialog() {

AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeActivity.this);
alertDialog.setTitle("Add new category");
alertDialog.setMessage("Please fill full information");

LayoutInflater inflater = this.getLayoutInflater();
View addMenuLayout = inflater.inflate(R.layout.add_new_menu_layout, null);

edtName = (MaterialEditText) addMenuLayout.findViewById(R.id.edt_name);
btnSelect = (Button) findViewById(R.id.btn_select);
btnUpload = (Button) findViewById(R.id.btn_upload);

btnSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage();
}
});

btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
}
});

alertDialog.setView(addMenuLayout);
alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp);

alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if(newCategory != null) {
category.push().setValue(newCategory);
}

}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}

最佳答案

您的按钮在 addMenuLayout 中膨胀

btnSelect = (Button)addMenuLayout. findViewById(R.id.btn_select);
btnUpload = (Button)addMenuLayout. findViewById(R.id.btn_upload);

关于java - 按钮在 Android 警报 View 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47938205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com