我有一个奇怪的问题,我无法解决。我正在尝试从我自己制作的服务中打开一个 ListActivity。ListActivity 接收来自服务的照片路径,该路径必须显示在列表中。这是服务的代码:
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast to=Toast.makeText(this, "entrando en servicio", 1000);
to.show();
accederFotosFolder();
crearArrayFotos();
Intent i=new Intent(this.getBaseContext(),Lista.class);
Bundle b=new Bundle();
b.putStringArrayList("imagenes",imagenes);
i.putExtras(b);
Toast t=Toast.makeText(this, ""+i, 1000);
t.show();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_FROM_BACKGROUND);
startActivity(i);
}
当我运行它时,我总是有一个空指针异常。
我检查了,没有什么是空的,不是捆绑包,不是 imagenes(这是一个ArrayList<String>
由 accederFotosFolder()+crearArrayFotos() 生成的 2 个元素)。意图也不是。如果我评论 startActivity(i) 一切正常,那么该行肯定是。我首先想到的是 mi ListActivity 出了点问题,但它从未进入它的代码,它只是停在 startActivity(i) 行。
有什么建议么?它快把我逼疯了。
更新
**That's the logcat output**
11-21 19:02:32.214: E/AndroidRuntime(28980): FATAL EXCEPTION: main
11-21 19:02:32.214: E/AndroidRuntime(28980): java.lang.RuntimeException: Unable to start service com.example.serviciofotos.Servicio@4166f550 with Intent { cmp=com.example.serviciofotos/.Servicio }: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-21 19:02:32.214: E/AndroidRuntime(28980): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2518)
11-21 19:02:32.214: E/AndroidRuntime(28980): at android.app.ActivityThread.access$1900(ActivityThread.java:134)
11-21 19:02:32.214: E/AndroidRuntime(28980): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
11-21 19:02:32.214: E/AndroidRuntime(28980): at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 19:02:32.214: E/AndroidRuntime(28980): at android.os.Looper.loop(Looper.java:154)
11-21 19:02:32.214: E/AndroidRuntime(28980): at android.app.ActivityThread.main(ActivityThread.java:4624)
11-21 19:02:32.214: E/AndroidRuntime(28980): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 19:02:32.214: E/AndroidRuntime(28980): at java.lang.reflect.Method.invoke(Method.java:511)
11-21 19:02:32.214: E/AndroidRuntime(28980): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
11-21 19:02:32.214: E/AndroidRuntime(28980): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
11-21 19:02:32.214: E/AndroidRuntime(28980): at dalvik.system.NativeStart.main(Native Method)
11-21 19:02:32.214: E/AndroidRuntime(28980): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-21 19:02:32.214: E/AndroidRuntime(28980): at android.app.ContextImpl.startActivity(ContextImpl.java:871)
11-21 19:02:32.214: E/AndroidRuntime(28980): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
11-21 19:02:32.214: E/AndroidRuntime(28980): at com.example.serviciofotos.Servicio.onStart(Servicio.java:53)
11-21 19:02:32.214: E/AndroidRuntime(28980): at android.app.Service.onStartCommand(Service.java:438)
11-21 19:02:32.214: E/AndroidRuntime(28980): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2501)
11-21 19:02:32.214: E/AndroidRuntime(28980): ... 10 more