[英]Cannot set EditText? Issue From dialog in a fragment - Strange - Android Java
I've come across an issue which I've never had before. 我遇到过一个我从未见过的问题。 I have a
Fragment
containing a button. 我有一个包含按钮的
Fragment
。 This button ( ViewBookingButton
) shows a popup dialog. 此按钮(
ViewBookingButton
)显示一个弹出对话框。 When the dialog opens I would like to set a string in an EditText
( AllBooking
). 当对话框打开时,我想在
EditText
( AllBooking
)中设置一个字符串。 Unfortunately it crashes and shows NullPointerExecption
. 不幸的是,它崩溃并显示
NullPointerExecption
。 What is going wrong? 出了什么问题? Here is my code:
这是我的代码:
ViewBookingButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
AlertDialog.Builder ViewBookingHelpBuilder = new AlertDialog.Builder(getActivity());
ViewBookingHelpBuilder.setTitle("view booking:");
LayoutInflater inflater = getActivity().getLayoutInflater();
View DialogLayout = inflater.inflate(R.layout.view_booking, null);
ViewBookingHelpBuilder.setView(DialogLayout);
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
AllBooking.setText("Hello World");//WHEN I REMOVE THIS THE DIALOG WORKS
ViewBookingHelpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
AlertDialog helpDialog = ViewBookingHelpBuilder.create();
helpDialog.show();
}
});
Change this 改变这个
TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);
to 至
TextView AllBooking = (TextView)DialogLayout.findViewById(R.id.AllBooking);
TextView
belongs to the inflated layout and `findViewById looks for the view in the current view hierarchy. TextView
属于膨胀布局,`findViewById在当前视图层次结构中查找视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.