I'm trying to add buttons to a DialogFragment
. My first approach was to declare the buttons on the fragment layout and then set OnClickListener
s on the OnCreateView
method. It did work, but as I was reading more about DialogFragment
I realized you can add buttons on the OnCreateDialog
method so I tried like this:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setCancelable(true)
.setPositiveButton(R.string.ok, null)
.setNeutralButton(R.string.print, null)
.setNegativeButton(R.string.cancel, null)
.create();
}
But I keep getting this exception:
07-22 12:40:41.883 30960-30960/com.sellcom.tracker E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:217)
at com.android.internal.app.AlertController.installContent(AlertController.java:237)
at android.app.AlertDialog.onCreate(AlertDialog.java:336)
at android.app.Dialog.dispatchOnCreate(Dialog.java:351)
at android.app.Dialog.show(Dialog.java:256)
at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:385)
at android.support.v4.app.Fragment.performStart(Fragment.java:1524)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:968)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
I've read a few tutorials but none says anything about it. The weird part is I'm not calling requestFeature()
at any point of the code.
This was my previous code:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
And this is where I inflate the custom view:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_receipt, container, false);
if (view != null) {
//Items of first view
flipper = (ViewFlipper) view.findViewById(R.id.view_flipper);
receiptNumber = (TextView) view.findViewById(R.id.receipt_number);
receiptDate = (TextView) view.findViewById(R.id.receipt_date);
issuedBy = (TextView) view.findViewById(R.id.issued_by);
receivedBy = (TextView) view.findViewById(R.id.received_by);
receiptSubtotal = (TextView) view.findViewById(R.id.receipt_subtotal);
receiptTaxes = (TextView) view.findViewById(R.id.receipt_tax);
receiptTotal = (TextView) view.findViewById(R.id.receipt_total);
productsList = (ListView) view.findViewById(R.id.products_list);
receiverSignature = (ImageView) view.findViewById(R.id.receiver_signature);
issuingSignature = (ImageView) view.findViewById(R.id.issuing_signature);
receiverName = (EditText) view.findViewById(R.id.receiver_name);
issuingName = (EditText) view.findViewById(R.id.issuing_name);
okButton = (Button) view.findViewById(R.id.ok_button);
cancelButton = (Button) view.findViewById(R.id.cancel_button);
printButton = (Button) view.findViewById(R.id.print_button);
//Items of second view
firstName = (EditText) view.findViewById(R.id.people_first_name);
lastName = (EditText) view.findViewById(R.id.people_last_name);
email = (EditText) view.findViewById(R.id.people_email);
phone = (EditText) view.findViewById(R.id.people_phone);
address1 = (EditText) view.findViewById(R.id.people_address_1);
address2 = (EditText) view.findViewById(R.id.people_address_2);
city = (EditText) view.findViewById(R.id.people_city);
state = (EditText) view.findViewById(R.id.people_state);
country = (EditText) view.findViewById(R.id.people_country);
zipCode = (EditText) view.findViewById(R.id.people_zip_code);
okButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
printButton.setOnClickListener(this);
receiverSignature.setOnClickListener(this);
issuingSignature.setOnClickListener(this);
switch (receiptType) {
case ReceiptAdapter.RECEIVING:
receiptItems = createReceivingList();
break;
case ReceiptAdapter.SALE:
receiptItems = createSalesList();
break;
case ReceiptAdapter.ORDER:
receiptItems = createOrderList();
receiptNumber.setVisibility(View.GONE);
receiptDate.setVisibility(View.GONE);
issuedBy.setVisibility(View.GONE);
receivedBy.setVisibility(View.GONE);
view.findViewById(R.id.issuing_layout).setVisibility(View.GONE);
break;
}
ReceiptAdapter receiptAdapter = new ReceiptAdapter(getActivity(), receiptType, receiptItems);
productsList.setAdapter(receiptAdapter);
}
return view;
}
All I did was change that for the code on the top. Am I missing something?