【问题标题】:how to implement onCompleteListener for DialogFragment如何为 DialogFragment 实现 onCompleteListener
【发布时间】:2013-10-03 17:54:25
【问题描述】:

我正在尝试为我的 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)

【问题讨论】:

    标签: android dialogfragment


    【解决方案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);

    【讨论】:

    • 谢谢 Vipul,我没有注意到。但是我已经收到了答案,但我也对你的答案投了赞成票。谢谢。
    【解决方案2】:

    做这些改变

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

    【讨论】:

    • 谢谢,我没注意到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 2013-06-11
    • 1970-01-01
    相关资源
    最近更新 更多