onServiceConnected没有得到所谓的,得到一个空指针异常

onServiceConnected没有得到所谓的,得到一个空指针异常

问题描述:

我制定了一个绑定的服务作为一个单独的项目&我试图从客户端访问服务,但我不知我得到onServiceConnected没有得到所谓的,得到一个空指针异常

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:在android.app.ActivityThread.main(ActivityThread.java:4725) 08-08 12:00:40.898 3206 3206 E AndroidRuntime:at java.lang.reflect.Method.invokeNative本地方法)

我怀疑onServiceConnected没有被调用。可以有人帮我解决这个问题。

由于事先

请看以下客户端代码:

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; 
} 
} 

由于您没有公布完整的源代码,包括服务就很难抢夺你的问题。您可以通过示例下载每种服务绑定类型的Demo Source,并获得一个快速的想法。该演示包含使用三种

1)使用的IBinder

2.绑定),使用Messenger的

3.绑定),使用AIDL

+0

绑定服务绑定我得到空logService.log_d(“LogClient”,“来自onClick()”的Hello)的指针异常;因为logService为空。 – 2012-08-08 12:31:13