質問

I have created a class taht extends from DialogFragment. I went with the custom layout and I added some buttons to this dialog.

I defined fields to the buttons but when I launch the dialog from the main activity I get nullreferenceexception.

Here is the dialog class:

public class PickRewardDialog extends DialogFragment{

private Button mButtonAddCoin;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mButtonAddCoin = (Button) getDialog().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();
        }
    });
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if(getDialog() != null){
        getDialog().setCanceledOnTouchOutside(false);
    }
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    Resources res = getActivity().getResources();
    Bundle bundle = getArguments();

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.dialog_pick_reward, null))
    //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();
}

}

and the layout:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFBB33"
    android:contentDescription="@string/app_name"
    android:scaleType="center"
    android:src="@drawable/dialog_title_pick_your_reward"/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Add 5 coins to your account!"
    android:id="@+id/button_dialog_reward_get_coin"
    android:layout_gravity="center_horizontal"/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Get a temorary boost on leaderboard!"
    android:id="@+id/button_doalog_reward_boost"/>

So the question is how can I set the clicklistener and do stuff with it?

役に立ちましたか?

解決

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();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top