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

}

}
}
...全文
121 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
e网情深e 2014-02-19
  • 打赏
  • 举报
回复
原因:conn已经释放了,再去释放就变为null,所以需先判断下conn是否已经为null

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧