2017-02-18 68 views
0

我在一個片段中創建一個界面類似以下內容:我怎樣才能在片段實現我的接口在另一個片段

public interface SGFCallBackInterface { 
    void itemSelected(Item item); 
} 

private SGFCallBackInterface mCallbackInterface; 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 

    if (context instanceof SGFCallBackInterface) { 
     mCallbackInterface = (SGFCallBackInterface) context; 
    } else { 
     throw new RuntimeException(context.toString() + " must implement SelectGdsFragment.SGFCallBackInterface"); 
    } 
} 

public void setSGFCallBackInterface(SGFCallBackInterface mCallbackInterface) { 
    this.mCallbackInterface = mCallbackInterface; 
} 

,我會實現,在另一個片段像下面

public class SaleMenageFragment extends Fragment implements SelectGdsFragment.SGFCallBackInterface { 

    ... 

    SelectGdsFragment selectGdsFragment = new SelectGdsFragment(); 
    selectGdsFragment.setSGFCallBackInterface(SaleMenageFragment.this); 

    ... 

} 

@Override 
public void onItemSelected(Item item) { 
    ... 
} 

但它仍然不工作,這是錯誤日誌:

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.leo.test, PID: 3853 
       java.lang.RuntimeException: [email protected] must implement SelectGdsFragment.SGFCallBackInterface 
        at com.leo.test.Control.Fragment.SaleManagement.SelectGdsClsFragment.onAttach(SelectGdsFragment.java:42) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019) 
        at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:779) 
        at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:819) 
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:660) 
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我認爲錯誤日誌意味着我的Inde xActivity沒有實現SelectGdsFragment.SGFCallBackInterface。

但我不想在IndexActivity中實現。
我想在SaleMenageFragment中實現它。

我該怎麼做?

+0

弗洛姆,非常感謝你! XDDD – Leo

回答

0

恐怕你必須實現它在你的Activity

看到該文檔

Communicating with Fragments

希望它有幫助!

+0

我懂了!非常感謝 !! – Leo

+0

很高興知道它有幫助! :) –

+0

參考鏈接:http://simpledeveloper.com/how-to-communicate-between-fragments-and-activities/ – Leo

0

只是刪除onAttach部分(上下文的背景下)方法是這樣的:

public interface SGFCallBackInterface { 
    void itemSelected(Item item); 
} 

private SGFCallBackInterface mCallbackInterface; 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 

    if (context instanceof SGFCallBackInterface) { 
     mCallbackInterface = (SGFCallBackInterface) context; 
    } // this wont throw an exception if the activity does not implement that interface 
} 

public void setSGFCallBackInterface(SGFCallBackInterface mCallbackInterface) { 
    this.mCallbackInterface = mCallbackInterface; 
} 
+0

對不起,這是行不通的。謝謝 !! – Leo

相關問題