如何绑定片段中的服务 [英] How do I bind a service from a fragment

查看:55
本文介绍了如何绑定片段中的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从片段中绑定服务,就像在活动中成功完成一样,但是当我尝试在服务上调用方法时,我收到了NullPointerException-显然是因为该服务为null.现在在onStart中绑定服务存在一些问题,还是我只是做错了?

I'm attempting to bind a service from a fragment the same way I have done succesfully in an activity, but when I try to call a method on the service I get a NullPointerException - Obviously because the service is null. Now is there some problem with binding to the service in onStart or am I simply doing it wrong?

@Override
public void onStart() {
    super.onStart();

    Intent intent = new Intent(getActivity(), LiteTrickService.class);
    getActivity().registerReceiver(receiver, new IntentFilter(LiteTrickService.BROADCAST_ACTION));
    getActivity().registerReceiver(receiver, new IntentFilter(LiteTrickService.BROADCAST_FAIL));
    getActivity().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

@Override
public void onStop() {
    super.onStop();
    getActivity().unbindService(mConnection);
    getActivity().unregisterReceiver(receiver);
    mBound = false;
}

抱歉.这是我没有对这个问题给予足够思考的错误.mConnection是一个ServiceConnection,看起来像这样:

edit: Sorry. That's my mistake for not giving this question enough thought. mConnection is a ServiceConnection and looks like this :

private ServiceConnection mConnection = new ServiceConnection() 
{

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};

Stacktrace:

Stacktrace :

01-03 15:21:22.355: E/AndroidRuntime(12360): FATAL EXCEPTION: main
01-03 15:21:22.355: E/AndroidRuntime(12360): java.lang.NullPointerException
01-03 15:21:22.355: E/AndroidRuntime(12360):    at lite.hattrick.players.PlayerRankingFragment.onOptionsItemSelected(PlayerRankingFragment.java:205)

这将是抛出异常的确切位置:案例POPULATE_ID:

And this is would be the exact place where the exception is thrown : case POPULATE_ID:

        if (hasData) {
            return false;
        }
        if(!mBound)
            getActivity().bindService(new Intent(getActivity().getApplicationContext(), LiteTrickService.class), mConnection, Context.BIND_AUTO_CREATE);
        mService.refreshPlayers(); // Null Pointer Exception as mService is null
        pBar.setVisibility(View.VISIBLE);
        return true;

推荐答案

我解决了这个问题.原来我忘了在清单中提供正确的软件包名称来提供服务声明.

I solved the problem. Turns out I had forgotten to provide service declaration in my manifest with its correct package name.

通过更改

<service android:name=".LiteTrickService" />

<service android:name="lite.hattrick.services.LiteTrickService" />

我解决了问题,服务现在可以按预期的方式连接.

I solved the problem and the service is now connecting as expected.

这篇关于如何绑定片段中的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆