Yesh Agnihotri Yesh Agnihotri - 20 days ago 10
Android Question

The app crashes as soon as i make my button clickable inside dialog fragment

Here is my code, If i remove the onclicklistener on button the app works just fine diaplaying the dailog i want to veiw.. but as soon i make it clickable the app stops working

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialogf, container,
false);
final Dialog dialog=getDialog();
dialog.setTitle("DialogFragment Tutorial");
Button button=(Button)dialog.findViewById(R.id.ok1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
return rootView;
}

Answer

if this R.id.ok1 is in your layout R.layout.dialogf file.

Then in code you should use

Button button=(Button)rootView.findViewById(R.id.ok1);

rootView instead of dialog