Service not registered
e网情深e 2014-02-19 09:32:38 [size=16px]02-19 12:59:45.841java.lang.IllegalStateException: Could not execute method of the activity
time(4469): at android.view.View$1.onClick(View.java:2144)
time(4469): at android.view.View.performClick(View.java:2485)
time(4469): at android.view.View$PerformClick.run(View.java:9080)
time(4469): at android.os.Handler.handleCallback(Handler.java:587)
time(4469): at android.os.Handler.dispatchMessage(Handler.java:92)
time(4469): at android.os.Looper.loop(Looper.java:123)
time(4469): at android.app.ActivityThread.main(ActivityThread.java:3683)
time(4469): at java.lang.reflect.Method.invokeNative(Native Method)
time(4469): at java.lang.reflect.Method.invoke(Method.java:507)
time(4469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
time(4469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
time(4469): at dalvik.system.NativeStart.main(Native Method)
time(4469): Caused by: java.lang.reflect.InvocationTargetException
time(4469): at java.lang.reflect.Method.invokeNative(Native Method)
time(4469): at java.lang.reflect.Method.invoke(Method.java:507)
time(4469): at android.view.View$1.onClick(View.java:2139)
time(4469): ... 11 more
time(4469):Caused_by:java.lang.IllegalArgumentException:Service_not_registered:null cn.itcast.service.MainActivity$MyServiceConnection@40536238
time(4469): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:891)
time(4469): at android.app.ContextImpl.unbindService(ContextImpl.java:890)
time(4469): at android.content.ContextWrapper.unbindService(ContextWrapper.java:352)
time(4469): at cn.itcast.service.MainActivity.unbind(MainActivity.java:40)
time(4469): ... 14 more
package cn.itcast.service;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private MyServiceConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// 开启服务
public void start(View v) {
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
public void stop(View v) {
Intent intent = new Intent(this, MyService.class);
stopService(intent);
}
// 绑定服务
public void bind(View v) {
Intent service = new Intent(this, MyService.class);
conn=new MyServiceConnection();
bindService(service, conn,BIND_AUTO_CREATE);
}
// 解除绑定服务
public void unbind(View v) {
unbindService(conn);
}
// 解除绑定服务
public void unbind(View v) {
if (conn != null) {
unbindService(conn);
conn = null;
}
}
private final class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
}