0

DialogFragment に onCompleteListener を実装しようとしていますが、フラグメントをアクティビティにアタッチする際にエラーが発生しています。

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

メソッド、ダイアログは表示されていますが、値は呼び出し元のアクティビティに返されません。アクティビティにも OnCompleteListener を実装しました。これが呼び出しアクティビティでの私の実装です

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();
}

}

そして私の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");
    }
}

どこが間違っているのでしょうか?スタック トレース エラーは onAttach() メソッドを指しています。これが最初の数行です

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)
4

2 に答える 2

1

そのはず

@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");
    }
}

電話するのを忘れた super.onAttach(activity);

于 2013-10-03T09:53:54.767 に答える