onServiceConnected not getting called , getting a null pointer exception

风流意气都作罢 提交于 2019-12-11 07:54:30

问题


I had developed a bound service as a separate project & I am trying to access the service from a client but I somehow I am getting

AndroidRuntime: FATAL EXCEPTION: main
08-08 12:00:40.898  3206  3206 E AndroidRuntime: java.lang.NullPointerException
08-08 12:00:40.898  3206  3206 E AndroidRuntime:    at    com.test.binder.BinderServiceActivity.onClick(BinderServiceActivity.java:61)
08-08 12:00:40.898  3206  3206 E AndroidRuntime:    at android.view.View.performClick(View.java:3526)
08-08 12:00:40.898  3206  3206 E AndroidRuntime:    at android.view.View$PerformClick.run(View.java:14139)
08-08 12:00:40.898  3206  3206 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:605)
08-08 12:00:40.898  3206  3206 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:92)
08-08 12:00:40.898  3206  3206 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:137)

08-08 12:00:40.898 3206 3206 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4725) 08-08 12:00:40.898 3206 3206 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)

I am suspecting that onServiceConnected is not getting called.Can someone help me resolving this.

Thanks in Advance,

Please find the client code below :

public class BinderServiceActivity extends Activity implements OnClickListener {
private static final String TAG = "LogActivity";
ILogService logService;
LogConnection conn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Request bind to the service
conn = new LogConnection(); // 
Intent intent = new Intent("com.test.binder.ILogService"); // 
intent.putExtra("version", "1.0"); // 
bindService(intent, conn, Context.BIND_AUTO_CREATE); // 

// Attach listener to button
((Button) findViewById(R.id.button1)).setOnClickListener(this);
}

class LogConnection implements ServiceConnection { // 

public void onServiceConnected(ComponentName name, IBinder service) { // 
  logService = ILogService.Stub.asInterface(service); // 
  Log.i(TAG, "connected");
}

public void onServiceDisconnected(ComponentName name) { // 
  logService = null;
  Log.i(TAG, "disconnected");
}

}

public void onClick(View button) {
try {

  logService.log_d("LogClient", "Hello from onClick()"); // 
  Message msg = new Message(Parcel.obtain()); // 
  msg.setTag("LogClient");
  msg.setText("Hello from inClick() version 1.1");
  logService.log(msg); // 
} catch (RemoteException e) { // 
  Log.e(TAG, "onClick failed", e);
}

}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroyed");

unbindService(conn); // 

logService = null;
}
}

回答1:


As you have not posted complete source including Service it would be difficult to snatch your problem. You can download the Demo Source for each type of Service Binding with example and get a quick idea. The demo contains Binding of Service using three types

1.) Binding using IBinder

2.) Binding using Messenger

3.) Binding using AIDL



来源:https://stackoverflow.com/questions/11844920/onserviceconnected-not-getting-called-getting-a-null-pointer-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!