Question

Am trying to implement an onCompleteListener for my DialogFragment.but am having errors attaching the fragment to the activity.If i leave out the

    public void onAttach(Activity activity) {
    try {
        this.mListener = (OnCompleteListener) activity;
    } catch (final ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnCompleteListener");
    }
}

method,the dialog is showing but the values are not passed back to the calling activity.I have implemented the OnCompleteListener for the Activity too. Here is my implementation in the calling activity

public class ViewMoreActivity extends FragmentActivity implements
    OnClickListener, BuySharesDialogFragment.OnCompleteListener {
-------------------------------
-------------------------------
    @Override
public void onComplete(String shares, String total_cost, String sharename,
        String paymentmode) {
    Toast.makeText(getApplicationContext(), shares, Toast.LENGTH_LONG)
            .show();
}

}

And my DialogFragment

public class BuySharesDialogFragment extends DialogFragment implements
    OnClickListener, OnItemSelectedListener {
    public static interface OnCompleteListener {
    public abstract void onComplete(String shares, String total_cost,
            String sharename, String paymentmode);
}

private OnCompleteListener mListener;
-------------------------------
-------------------------------
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_cancel:
        getDialog().dismiss();
        break;
    case R.id.dialogbutton_buy_shares:
        this.mListener.onComplete(String.valueOf(shares), total_cost
                .getText().toString(), company_name.getText().toString(),
                paymentmethod);
        break;
    }
}


public void onAttach(Activity activity) {
    try {
        this.mListener = (OnCompleteListener) activity;
    } catch (final ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnCompleteListener");
    }
}

Where might i be going wrong?The stack trace error points to the onAttach() method. Here's the first few lines

10-03 12:38:26.890: E/AndroidRuntime(5903): FATAL EXCEPTION: main
10-03 12:38:26.890: E/AndroidRuntime(5903): android.support.v4.app.SuperNotCalledException: Fragment BuySharesDialogFragment{405a5e38 #2 fragment_edit_name} did not call through to super.onAttach()
`10-03 12:38:26.890`: E/AndroidRuntime(5903):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:869)
10-03 12:38:26.890: E/AndroidRuntime(5903):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
Was it helpful?

Solution 2

Do this changes

public void onAttach(Activity activity) {
     super.onAttach(activity); //UPDATE HERE
    try {
        this.mListener = (OnCompleteListener) activity;
    } catch (final ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnCompleteListener");
    }
}

OTHER TIPS

It should be

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        this.mListener = (OnCompleteListener) activity;
    } catch (final ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
    }
}

You forgot to call super.onAttach(activity);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top