onServiceConnected没有得到所谓的,得到一个空指针异常 [英] onServiceConnected not getting called , getting a null pointer exception
问题描述
我已经开发了一个绑定的服务作为一个单独的项目和放大器;我试图从客户端访问服务,但我不知我正在
AndroidRuntime:致命异常:主要
十二月8日至八日:00:40.898 3206 3206电邮AndroidRuntime:显示java.lang.NullPointerException
十二月8日至八日:00:40.898 3206 3206电邮AndroidRuntime:在com.test.binder.BinderServiceActivity.onClick(BinderServiceActivity.java:61)
十二月8日至八日:00:40.898 3206 3206电邮AndroidRuntime:在android.view.View.performClick(View.java:3526)
十二月8日至八日:00:40.898 3206 3206电邮AndroidRuntime:在android.view.View $ PerformClick.run(View.java:14139)
十二月8日至八日:00:40.898 3206 3206电邮AndroidRuntime:在android.os.Handler.handleCallback(Handler.java:605)
十二月8日至八日:00:40.898 3206 3206电邮AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:92)
十二月8日至八日:00:40.898 3206 3206电邮AndroidRuntime:在android.os.Looper.loop(Looper.java:137)
12月8日至八日:00:40.898 3206 3206电邮AndroidRuntime:在android.app.ActivityThread.main(ActivityThread.java:4725)
十二月8日至八日:00:40.898 3206 3206电邮AndroidRuntime:在java.lang.reflect.Method.invokeNative(本机方法)
我怀疑onServiceConnected没有得到called.Can有人能帮我解决这个。
在预先感谢
请在下面找到客户端code:
公共类BinderServiceActivity扩展活动实现OnClickListener {
私有静态最后弦乐TAG =LogActivity;
ILogService logService;
LogConnection康涅狄格州;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);//请求绑定到服务
康恩=新LogConnection(); //
意向意图=新意图(com.test.binder.ILogService); //
intent.putExtra(版本,1.0); //
bindService(意向,康涅狄格州,Context.BIND_AUTO_CREATE); ////连接监听器按钮
((按钮)findViewById(R.id.button1))setOnClickListener(本);
}类LogConnection实现ServiceConnection {//公共无效onServiceConnected(组件名名称,服务的IBinder){//
logService = ILogService.Stub.asInterface(服务); //
Log.i(TAG,连接);
}公共无效onServiceDisconnected(组件名名){//
logService = NULL;
Log.i(TAG,断开连接);
}}公共无效的onClick(视图按钮){
尝试{ logService.log_d(LogClient,你好来自的onClick()); //
消息味精=新的Message(Parcel.obtain()); //
msg.setTag(LogClient);
msg.setText(你好从inClick()1.1版);
logService.log(MSG); //
}赶上(RemoteException的E){//
Log.e(TAG的onClick失败,E);
}}@覆盖
保护无效的onDestroy(){
super.onDestroy();
Log.d(TAGonDestroyed);unbindService(康涅狄格州); //logService = NULL;
}
}
由于您没有公布完整的源代码,包括服务就很难抢夺你的问题。您也可以下载 演示源
为每种类型的与服务绑定的例子,并得到一个快速的想法。该演示包含使用三种类型的装订服务的
1)结合使用的IBinder
2)使用绑定使者
3)绑定使用AIDL
块引用>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; } }
解决方案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 types1.) Binding using IBinder
2.) Binding using Messenger
3.) Binding using AIDL
这篇关于onServiceConnected没有得到所谓的,得到一个空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!