从未调用OnServiceConnected

时间:2014-08-05 20:50:30

标签: android service

我在这个网站上搜索了几十个关于这个完全相同问题的链接,但对于我的生活,我似乎无法弄明白。我只是将我的活动绑定到我的服务,但是当我尝试调用该服务的方法时(是的,它是公共的),我得到一个NullPointerException。这是我的代码。请帮忙。

这是我的服务本身:

public class ParseService extends Service {

public static ParseUser user;

private final IBinder binder = new LocalBinder();

public class LocalBinder extends Binder {
    public ParseService getService() {
        return ParseService.this;
    }
}

@Override
public IBinder onBind(Intent intent) {
    return binder;
}

这是我的活动代码,它绑定到服务:

@Override
protected void onStart() {
    super.onStart();
    bindToService();
}

private void bindToService() {
    Log.v("Miles", "bind to service");
    Intent intent = new Intent(this, ParseService.class);
    getApplicationContext().bindService(intent, serviceConnection, Context.BIND_DEBUG_UNBIND);
}

private ServiceConnection serviceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        Log.v("Miles", "on service connected");
        parseService = ((ParseService.LocalBinder)binder).getService();
        serviceBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
        Log.v("Miles", "on service disconnected");
        serviceBound = false;
    }
};

和NullPointerException日志。当我单击Activity中的菜单项时,它会在我的服务ParseService中调用一个方法。这种方法目前只是一个虚拟的“LOG”语句,纯粹是出于测试目的。

08-05 16:56:57.788:E / AndroidRuntime(1205):致命异常:主要 08-05 16:56:57.788:E / AndroidRuntime(1205):处理:max.out,PID:1205 08-05 16:56:57.788:E / AndroidRuntime(1205):java.lang.NullPointerException 08-05 16:56:57.788:E / AndroidRuntime(1205):at maxOut.StatsScreen.onOptionsItemSelected(StatsScreen.java:125) 08-05 16:56:57.788:E / AndroidRuntime(1205):在android.app.Activity.onMenuItemSelected(Activity.java:2608) 08-05 16:56:57.788:E / AndroidRuntime(1205):在android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:373) 08-05 16:56:57.788:E / AndroidRuntime(1205):at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1012) 08-05 16:56:57.788:E / AndroidRuntime(1205):at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735) 08-05 16:56:57.788:E / AndroidRuntime(1205):at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) 08-05 16:56:57.788:E / AndroidRuntime(1205):at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) 08-05 16:56:57.788:E / AndroidRuntime(1205):at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546) 08-05 16:56:57.788:E / AndroidRuntime(1205):at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:129) 08-05 16:56:57.788:E / AndroidRuntime(1205):在android.view.View.performClick(View.java:4466) 08-05 16:56:57.788:E / AndroidRuntime(1205):在android.view.View $ PerformClick.run(View.java:18537) 08-05 16:56:57.788:E / AndroidRuntime(1205):在android.os.Handler.handleCallback(Handler.java:733) 08-05 16:56:57.788:E / AndroidRuntime(1205):在android.os.Handler.dispatchMessage(Handler.java:95) 08-05 16:56:57.788:E / AndroidRuntime(1205):在android.os.Looper.loop(Looper.java:136) 08-05 16:56:57.788:E / AndroidRuntime(1205):在android.app.ActivityThread.main(ActivityThread.java:5102) 08-05 16:56:57.788:E / AndroidRuntime(1205):at java.lang.reflect.Method.invokeNative(Native Method) 08-05 16:56:57.788:E / AndroidRuntime(1205):at java.lang.reflect.Method.invoke(Method.java:515) 08-05 16:56:57.788:E / AndroidRuntime(1205):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 08-05 16:56:57.788:E / AndroidRuntime(1205):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 08-05 16:56:57.788:E / AndroidRuntime(1205):at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案