您的位置:首页 > 其它

写Service Demo的时候遇到的一个问题小结一下

2016-01-14 10:55 155 查看
我写了一个  Service 的Demo,但是将启动Service的Intent声明为主Activity的成员变量时,

public class MyServiceActivity extends Activity {
private Button startSer1;
private Button stopSer1;
private Button startSer2;
private Button stopSer2;
public static TextView log;
private Intent intent = new Intent(MyServiceActivity.this, MyService.class);

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startSer1 = (Button) findViewById(R.id.startSer1);
stopSer1 = (Button) findViewById(R.id.stopSer1);

startSer2 = (Button) findViewById(R.id.startSer2);
stopSer2 = (Button) findViewById(R.id.stopSer2);

log = (TextView) findViewById(R.id.log);

startSer1.setOnClickListener(btnListener);
stopSer1.setOnClickListener(btnListener);

startSer2.setOnClickListener(btnListener);
stopSer2.setOnClickListener(btnListener);
}

private OnClickListener btnListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startSer1:
updateLog("Start Service 1 pressed");
startService(intent);
break;
case R.id.startSer2:
updateLog("Start Service 2 pressed");
startService(intent);
break;
case R.id.stopSer1:
updateLog("Stop Service 1 pressed");
stopService(intent);
break;
case R.id.stopSer2:
updateLog("Stop Service 2 pressed");
stopService(intent);
break;
default:
break;
}
}
};

public static void updateLog(String text) {
CharSequence ch = log.getText();
log.setText(((ch == null || ch.length() == 0) ? text : ch.toString()
+ "\r\n" + text));
}
}

 
系统总是在启动的报异常:

08-08 12:03:22.894: ERROR/AndroidRuntime(2662): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.archer.rainbow/com.archer.rainbow.MyServiceActivity}: java.lang.NullPointerException
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1570)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1664)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.app.ActivityThread.access$1600(ActivityThread.java:118)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:949)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.os.Looper.loop(Looper.java:130)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.app.ActivityThread.main(ActivityThread.java:3711)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at java.lang.reflect.Method.invoke(Method.java:507)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at dalvik.system.NativeStart.main(Native Method)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662): Caused by: java.lang.NullPointerException
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.content.ComponentName.<init>(ComponentName.java:75)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.content.Intent.<init>(Intent.java:2936)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at com.archer.rainbow.MyServiceActivity$1.<init>(MyServiceActivity.java:40)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at com.archer.rainbow.MyServiceActivity.<init>(MyServiceActivity.java:38)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at java.lang.Class.newInstanceImpl(Native Method)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at java.lang.Class.newInstance(Class.java:1409)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1562)
08-08 12:03:22.894: ERROR/AndroidRuntime(2662):     ... 11 more

 

一开始,我还以为是我的哪个变量没有初始化呢,结果,看来看去,一共没几个变量啊,而且都应该正常赋值了,所以就很是纳闷了。但这个问题不解决不行啊,所以我就在一EOE论坛里发贴求高手解答。虽然有几个热心的网友帮忙给了些建议,但都不奏效啊。没办法,自己再想吧。后来,我换了一种写法: 

private final Intent intent = new Intent();
intent.setComponent(new ComponentName(MyServiceActivity.this.getPackageName(), MyService.class.toString()));

 

并在第二句打了个断点进行了DEBUG,按F5进去之后发现:

public String getPackageName() {
return mBase.getPackageName();
}

 

中的 mBase 竟然为 null ,无语了。这个方法可是ContextWrapper里的啊,Activity继承自它,怎么会没有传这个值给它呢?后来发现,Activity在OnCreate之前进行init的时候,调用了 

public ContextWrapper(Context base) {
mBase = base;
}

 

此时,传进去的 base 即为 null,所以这样一来,便出现上面的问题。

为了避免这样的问题,需要在Activity在执行到onCreate()方法之后(包括在onCreate()方法),对该Intent进行赋值即可。

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startSer1 = (Button) findViewById(R.id.startSer1);
stopSer1 = (Button) findViewById(R.id.stopSer1);

startSer2 = (Button) findViewById(R.id.startSer2);
stopSer2 = (Button) findViewById(R.id.stopSer2);

log = (TextView) findViewById(R.id.log);

startSer1.setOnClickListener(btnListener);
stopSer1.setOnClickListener(btnListener);

startSer2.setOnClickListener(btnListener);
stopSer2.setOnClickListener(btnListener);

intent = new Intent(MyServiceActivity.this, MyService.class);
}

 

通过这个Demo,吸取到了一个教训:需要使用Context方面的信息,最好在onCreate()方法之后(包括之)。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: