android bindService() NullPointerException

android app开发中 下面的代码报错误 ,bindService() NullPointerException


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

public

class

MainActivity
extends

Activity
implements

OnClickListener{

Button
but;

Button
but2;

LocalBinder
binder;

@Override

protected

void

onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

but
= (Button) findViewById(R.id.button1);

but.setOnClickListener(this);

but
= (Button) findViewById(R.id.button2);

}

ServiceConnection
connection =
new

ServiceConnection(){

@Override

public

void

onServiceConnected(ComponentName arg0, IBinder arg1) {

Log.d("D1",
"connecting");

binder
= (LocalBinder) arg1;

}

@Override

public

void

onServiceDisconnected(ComponentName arg0) {

Log.d("D1",
"disconnecting");

}

};

public

static

class

ServiceTest
extends

Service{

@Override

public

IBinder onBind(Intent arg0) {

//
TODO Auto-generated method stub

return

new

LocalBinder();

}

public

class

LocalBinder
extends

Binder{

public

void

toast(){

Toast.makeText(getApplicationContext(),
"hope
this works"
,
Toast.LENGTH_LONG).show();

}

}

}

@Override

public

void

onClick(View arg0) {

Intent
i =
new

Intent(getApplicationContext(), ServiceTest.LocalBinder.
class);

bindService(i,
connection, Context.BIND_AUTO_CREATE);

Toast.makeText(getApplicationContext(),
"bind
completed"
,
Toast.LENGTH_LONG).show();

}

public

void

binderMethod(View v){

binder.toast();

}

}

}

我自己分析了下感觉是 onServiceConnected 没有执行,所以才会引起空指针

在but2执行调用 binder.toast() 的时候 会绑定 service 和 activity

当我点击btn2的时候才抛出这个异常.

为什么 onServiceConnected 没有执行?

处理方法

intent 在调用service的时候并没有绑定service


1

Intent
i =
new

Intent(getApplicationContext(), ServiceTest.
class);

原文地址:http://www.itmmd.com/201411/95.html

该文章由 萌萌的IT人 整理发布,转载须标明出处。

时间: 2024-08-09 14:41:20

android bindService() NullPointerException的相关文章

Android bindService 的进程被 kill 掉之后会怎样

今天突发奇想,想知道:Android中,A bindService B之后,如果 A 或者 B 被 kill 掉了会怎么样呢? 1. kill A,毫无疑问应该是: B will not be stopped. 2. kill B,那么会是怎么样呢? A will receive onServiceDisconnected call back. And A will reconnect B ? or A will re-bindService B ? or A will receive onSe

Android bindService流程

下图描述从一个Application A如何bind在Application B中的Service. The picture above is the flow chart for how the source Application bind a target service Major includes four steps: (1) Inform AMS to start the bind, flow chart in blue; (2) Start the target applicat

Android - NullPointerException

Android中的空指针,感觉出现率,比普通的java出现的概率多得多,今天我无意中发现的NullPointerException,还是想记录下来. 我们知道, 我们的控件一般定义在layout.xml中,所以我们使用findViewById方法去寻找控件的时候,虽然不是通过 R.layout.activity,一般默认生成的代码会在Activity中的onCreate方法中: setContentView(R.layout.activity); 不小心将该行注释了,发现Android报Null

Android service ( 一 ) 三种开启服务方法

一. Service简介 Service是android 系统中的四大组件之一(Activity.Service.BroadcastReceiver.ContentProvider),它跟 Activity的级别差不多,但不能自己运行只能后台运行,并且可以和其他组件进行交互.service可以在很多场合的应用中使用,比如播放多媒体的 时候用户启动了其他Activity这个时候程序要在后台继续播放,比如检测SD卡上文件的变化,再或者在后台记录你地理信息位置的改变等等,总之服务总 是藏在后台的. S

新建android系统服务

一.Android系统服务 Android提供了很多系统服务:如ActivityManger,PowerManger,WindowManger,WifiManger等等. 这些服务都是系统启动开始就一直常驻的,如果app需要使用相应的系统服务(如获取当前wifi的状态),则需要通过Binder去访问系统服务. 例如:WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 二.

Android Service(二)

上一篇文章中我们启动service的方法是: startService() intent = new Intent("android.startService");//区分服务类型 Bundle bundle = new Bundle(); bundle.putInt("op", op); intent.putExtras(bundle); startService(intent); //此处也可以用 intent.setClass(this,StartMusicS

Android四大基本组件之 Service

一.Service的简介 1.Service介绍和作用 Service是Android系统中的四大组件之一,它是一种长生命周期的,没有可视化界面,运行于后台的一种服务程序.比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当退出播放音乐的应用,如果不用Service,我 们就听不到歌了,所以这时候就得用到Service了. 2.Service生命周期 Service的生命周期并不像Activity那么复杂,它只继承了onCreate(),onStart(),onDestroy()三个方法,

Android Service组件在新进程绑定(bindService)过程

1.首先看两个例子 (1)进程内 Client端 public class CounterService extends Service implements ICounterService { ...... public class CounterBinder extends Binder { public CounterService getService() { return CounterService.this; } } ...... } Server端 public class Ma

理解Android的startservice和bindservice(转)

一.首先,让我们确认下什么是service? service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比较高,它比处于前台的应用优先级低,但是比后台的其他应用优先级高,这就决定了当系统因为缺少内存而销毁某些没被利用的资源时,它被销毁的概率很小哦. 二.那么,什么时候,我们需要使用service呢?         我们知道,service是运行在后台的应用,对于用户来说失去了被关注的焦点.这就跟我们打开了音乐播放之后