DialogFragment侦听器在另一个片段中的实现

时间:2015-01-18 16:46:42

标签: android android-fragments listener android-dialogfragment

我有一个调用DialogFragment的Fragment,但它在setOnSensorRateChangeListener中崩溃。

这就是我从Fragment调用DialogFragment的方法

FragmentManager fm = getFragmentManager();
SensorRateChangeDlg dialogFragment = new SensorRateChangeDlg();
dialogFragment.setOnSensorRateChangeListener(this); // here it crashes ->  java.lang.IllegalStateException: You can not set Dialog's OnCancelListener or OnDismissListener
dialogFragment.show(fm, "rate");

这是DialogFragment中的实现

public interface SensorRateChangeListener {
  public int getRate();
  public void onRateChange(int delay_picked);
}

我在片段中有这个

public class SensorViewFragment extends Fragment implements
    SensorEventListener, SensorRateChangeDlg.SensorRateChangeListener {

***************************************
// Provide the current sensor's rate setting this method is
// defined by the SensorRateChangeDlg.SensorRateChangeListener interface
@Override
public int getRate() {
    return settings.getRate();
}

// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the SensorRateChangeDlg.SensorRateChangeListener interface
@Override
public void onRateChange(int rate_picked) {
    // User touched the dialog's positive button

    // Remember setting at chosen rate
    settings.setRate(rate_picked);
    applySettings();

    return;
}

0 个答案:

没有答案