2013-10-03 65 views
0

我試圖爲我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) 

回答

0

做這個改變

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

謝謝我沒有注意到。 –

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

+0

感謝VIPUL,我沒有注意到這一點。不過,我已經收到了一個答案,但我已經投了你的答案了阿克斯,我欣賞。 –

相關問題