简体   繁体   中英

How to use Interface on Fragment

In my application i want implement some interface into fragment !
When use this interface i write this for add listeners , but show me error and not allow me for use this!
I write below codes into fragment :

public class ServicesFragment extends Fragment implements IabHelper.OnIabSetupFinishedListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_services, container, false);

    public class LoginCheckServiceConnection implements ServiceConnection {

        public void onServiceConnected(ComponentName name, IBinder boundService) {
            service = ILoginCheckService.Stub.asInterface((IBinder) boundService);
            try {
                boolean isLoggedIn = service.isLoggedIn();
                if (isLoggedIn) {
                    iabHelper = new IabHelper(context, bazaarRSA);
                    iabHelper.startSetup(this);
                } else {
                    if (Constants.isPackageInstalled(Constants.BAZAAR_PAYMENT_PACKAGE, packageManager)) {
                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.setComponent(new ComponentName(Constants.BAZAAR_PAYMENT_PACKAGE, Constants.BAZAAR_LOGIN_ACTIVITY));
                        startActivity(intent);
                    } else {
                        Toast.makeText(context, "Not installed market on your device", Toast.LENGTH_SHORT).show();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void onServiceDisconnected(ComponentName name) {
            service = null;
        }
    }
}

In this code : iabHelper.startSetup(this); when use this show me error!

How can i fix it?

Method startSetup signature is .

public void startSetup(final OnIabSetupFinishedListener listener)

Problem:- You are passing this which will give the reference of immediate parent class which is LoginCheckServiceConnection in this case.

Solution:- Use ClassName.this to get outer class reference cause your outer class already implementing OnIabSetupFinishedListener .

iabHelper.startSetup(ServicesFragment.this);

If you initialize this inside the onCreateView() or no nested codes then iabHelper.startSetup(this) can implicitly serves your context as usual. Also the reference of immediate parent class which is LoginCheckServiceConnection in this case.

But if you deep drive into nested code then only this can not serves implicitly your context as usual. For that you can explicitly write that iabHelper.startSetup(ServicesFragment.this);

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