简体   繁体   中英

How can I implement in fragment with my interface in another fragment

I created a interface in a fragment like following:

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

and I would implement that in another fragment like the following

public class SaleMenageFragment extends Fragment implements SelectGdsFragment.SGFCallBackInterface { 

    ...

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

    ...

}

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

But it's still not working, this is the error log:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.leo.test, PID: 3853
              java.lang.RuntimeException: com.leo.test.Control.IndexActivity@2a66aece 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)

I think the error log means my IndexActivity is not implementing SelectGdsFragment.SGFCallBackInterface.

But I don't want to implement that in IndexActivity.
I want to implement that in SaleMenageFragment.

How can I do that?

Just remove the part in onAttach(Context context) method like this:

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

I am afraid you have to implement it in your Activity

See the doc

Communicating with Fragments

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM