我試圖爲我DialogFragment.but我有錯誤的片段連接到activity.If我離開了如何實現onCompleteListener爲DialogFragment
public void onAttach(Activity activity) {
try {
this.mListener = (OnCompleteListener) activity;
} catch (final ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnCompleteListener");
}
}
方法來實現的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)
謝謝我沒有注意到。 –