我在主活动中托管了一个对话框,要求输入名称。如果用户在对话框中按下确定,则名称将传递给托管活动并打印为textView。
问题是在textView中发送了空值。
这是我的对话框代码 -
public class dialog extends DialogFragment {
public interface myInter{
public void positive(String name);
public void negative();
}
myInter ob;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder ad=new AlertDialog.Builder(getActivity());
LayoutInflater li=getActivity().getLayoutInflater();
final View v=li.inflate(R.layout.dialog,null);
ad.setView(li.inflate(R.layout.dialog,null));
ad.setTitle("This is a dialog box");
ad.setMessage("HI, Do you agree with our terms and conditions ?");
ad.setPositiveButton("Alright", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface di,int id){
EditText et=(EditText)v.findViewById(R.id.editText1);
ob.positive(et.getText().toString());
}});
ad.setNegativeButton("Nope", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface di,int i){
ob.negative();
}});
return ad.create();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
ob=(myInter)activity;
}
catch(Exception e){
throw new ClassCastException();
}
}
}
我在主要活动中收到空值 请帮助。
答案 0 :(得分:2)
因为来自行,
final View v=li.inflate(R.layout.dialog,null);
ad.setView(li.inflate(R.layout.dialog,null));
您有两个布局对话框视图参考,
改变它,
final View v=li.inflate(R.layout.dialog,null);
ad.setView(v);