Reputation: 774
I have a dialog box hosted in the main activity which asks for a name . If user presses ok in the dialog then the name is passed to the hosting activity and printed as textView.
The problem is that null value is being sent in the textView.
This is my dialog code -
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();
}
}
}
I am receiving null values in my main activity.
Kindly help.
Upvotes: 0
Views: 105
Reputation: 109237
Because from lines,
final View v=li.inflate(R.layout.dialog,null);
ad.setView(li.inflate(R.layout.dialog,null));
You have two reference of layout dialog view,
change it wih,
final View v=li.inflate(R.layout.dialog,null);
ad.setView(v);
Upvotes: 2