The dialog is not available in the onCreate()
callback of the Fragment
so the mButtonAddCoin
reference will be null. Instead, setup the Button
in the onCreateDialog()
callback where you actually inflate the dialog's view:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Resources res = getActivity().getResources();
Bundle bundle = getArguments();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_pick_reward, null);
mButtonAddCoin = (Button) dialogView.findViewById(R.id.button_dialog_reward_get_coin);
mButtonAddCoin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getDialog().getContext().getApplicationContext(), "You will" +
" receive 5 coins!", Toast.LENGTH_SHORT).show();
}
});
builder.setView(dialogView);
//Positive button
.setPositiveButton(R.string.dialog_choose_reward_positive_button,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//do nothing or Dismiss()
}
}
);
return builder.create();
}