gpt4 book ai didi

android - 从警报对话框启动自定义对话框 - NullPointer 错误

转载 作者:行者123 更新时间:2023-11-29 18:14:06 25 4
gpt4 key购买 nike

我正在尝试通过在警报对话框中按下按钮来启动自定义对话框。用户在打开 redeemAlertDialog 的主 UI 中按下一个按钮,此对话框询问用户是否确定要继续此操作。如果他们单击"is",那么我想打开我的自定义对话框。但是,启动我的自定义对话框会导致应用程序崩溃。 Logcat 告诉我在行 *text.setText("Blah Blah"/merchantName/);* 上有一个空指针错误,但是如果我注释掉这一行,我在行 button.setOnClickListener(new OnClickListener() {如果我注释掉这两行,那么它就起作用了。在四处挖掘之后,我认为我的问题与我在创建自定义对话框时将其关联的上下文有关,但我无法修复它。如果有人能指出我哪里出错了,我将不胜感激。我的代码如下。

已解决在我的 onCreate 方法中,我将 mContext 的定义从 mContext = getApplicationContext(); 更改为至 mContext = this;由于某些原因 couponDialog = new Dialog(mContext);不喜欢 getApplicationContect() 给出的内容;

    private void redeem() {
AlertDialog.Builder redeemAlerDialogBuilder = new AlertDialog.Builder(this);
redeemAlerDialogBuilder.setMessage("Are you sure you want to redeem?")
.setCancelable(false) //User must select a button, can't use the back button
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Do something to launch a redeem dialog
//openCouponDialog();
couponDialog = new Dialog(mContext);
couponDialog.setContentView(R.layout.redeem_layout);
couponDialog.setTitle("Freebie Coupon");
couponDialog.setCancelable(false); //User should only be able to exit dialog by clicking done

TextView text = (TextView) findViewById(R.id.redeemMerchantName);
text.setText("Blah Blah"/*merchantName*/);

ImageView image = (ImageView) findViewById(R.id.couponImage);
//Set merchant coupon image here - need to download this from server when merchant is first added

Button button = (Button) findViewById(R.id.redeemDialogCloseButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});

couponDialog.show();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); //Cancel redeem
}
});
redeemAlertDialog = redeemAlerDialogBuilder.create();
redeemAlertDialog.show();
}

最佳答案

代替:

Button button = (Button) findViewById(R.id.redeemDialogCloseButton);

TextView text = (TextView) findViewById(R.id.redeemMerchantName);

使用

Button button = (Button) couponDialog.findViewById(R.id.redeemDialogCloseButton);
TextView text = (TextView) couponDialog.findViewById(R.id.redeemMerchantName);

希望这能行得通

关于android - 从警报对话框启动自定义对话框 - NullPointer 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9116391/

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