我尝试使用自定义布局创建AlertDialog。当我尝试从内部按钮关闭对话框时出现问题,所以我之前将其声明为final。虽然正确引用了元素id,但它会抛出一个错误,说自定义的layoutelement为null。这是代码:
Context ctx = MyActivity.this;
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService( ctx.LAYOUT_INFLATER_SERVICE );
View dialogView = inflater.inflate(R.layout.mkdialog_layout, null);
builder.setView(dialogView);
final AlertDialog myalertdialog = builder.create();
Button okbtt = (Button)myalertdialog.findViewById(R.id.mkdialog_okbtt);
Button cancelbtt = (Button)myalertdialog.findViewById(R.id.mkdialog_cancelbtt);
TextView title = (TextView)myalertdialog.findViewById(R.id.mkdialog_titletv);
title.setText("Are you sure you want to log out?");
okbtt.setOnClickListener(new View.OnClickListener(){
public void onClick(View id) {
myalertdialog.dismiss();
Intent i = new Intent(getBaseContext(), LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}});
cancelbtt.setOnClickListener(new View.OnClickListener(){
public void onClick(View id) {
myalertdialog.dismiss();
}});
myalertdialog.show();
错误:
Process: com.mk.companionapp, PID: 22045 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.mk.companionapp.activities.ConfigurationActivity$2.onClick(ConfigurationActivity.java:185)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
提前致谢
答案 0 :(得分:1)
用此更新对话框代码。
Context ctx = getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService( ctx.LAYOUT_INFLATER_SERVICE );
View dialogView = inflater.inflate(R.layout.mkdialog_layout, null);
builder.setView(dialogView);
final AlertDialog myalertdialog = builder.create();
Button okbtt = (Button)dialogView.findViewById(R.id.mkdialog_okbtt);
Button cancelbtt = (Button)dialogView.findViewById(R.id.mkdialog_cancelbtt);
TextView title = (TextView)dialogView.findViewById(R.id.mkdialog_titletv);
title.setText("Are you sure you want to log out?");
okbtt.setOnClickListener(new View.OnClickListener(){
public void onClick(View id) {
myalertdialog.dismiss();
Intent i = new Intent(getBaseContext(), LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}});
cancelbtt.setOnClickListener(new View.OnClickListener(){
public void onClick(View id) {
myalertdialog.dismiss();
}});
myalertdialog.show();
您使用错误的视图初始化了view_items。
答案 1 :(得分:1)
而不是myalertdialog
,您应该使用dialogView
。因此,要使代码正常工作,您必须替换
TextView title = (TextView)myalertdialog.findViewById(R.id.mkdialog_titletv);
以下
TextView title = (TextView) dialogView.findViewById(R.id.mkdialog_titletv);
答案 2 :(得分:1)
正如其他人所说,使用dialogView来查找视图而不是myalertdialog,并且将活动上下文传递给Dialog而不是应用程序上下文。因此,请进行以下更改。
上下文ctx = YourActivity.this;
TextView title =(TextView)myalertdialog.findViewById(R.id.mkdialog_titletv);
到
TextView title =(TextView)dialogView.findViewById(R.id.mkdialog_titletv);
答案 3 :(得分:1)
使用它。您必须使用Activity来创建AlertDialog。 不要使用ApplicationContext
AlertDialog.Builder builder = new AlertDialog.Builder(this);
答案 4 :(得分:0)
TextView title = (TextView)myalertdialog.findViewById(R.id.mkdialog_titletv);
到
TextView title = (TextView) dialogView.findViewById(R.id.mkdialog_titletv);
答案 5 :(得分:0)
使用以下方法将阻止Alert在我们单击任何按钮后立即关闭,并根据我们的业务逻辑提供关闭选项。
以下示例提醒在EditText
custom_dialog
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialog.setView(dialogView);
final EditText edt = (EditText)dialogView.findViewById(R.id.email_forgot);
dialog.setView(dialogView);
dialog.setPositiveButton("Ok",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
dialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
AlertDialog forgotAlertDialog = dialog.create();
forgotAlertDialog.show();
forgotAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//your positive action
}
});
要关闭警报,请按照逻辑
执行以下操作if (forgotAlertDialog.isShowing()) {
forgotAlertDialog.dismiss();
}