Service.startActivity(intent)抛出空指针异常

时间:2012-11-21 17:28:46

标签: android service nullpointerexception

我有一个奇怪的问题,我无法解决。我正在试图从我自己创建的服务中打开ListActivity。ListActivity接收来自服务的foto路径,必须在列表中显示。这是服务的代码:

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);
}

当我运行这个时,我总是有一个空指针Exception。

我检查过,没有任何东西是null,不是bundle,而不是imagenes(这是一个带有由accederFotosFolder()+ crearArrayFotos()生成的2个元素的ArrayList)。意图也不是。如果我评论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

3 个答案:

答案 0 :(得分:3)

使用当前服务上下文或getApplicationContext()代替getBaseContext()从服务启动ListActivity:

Intent i=new Intent(Your_Service_Name.this,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);

答案 1 :(得分:1)

解!!!!!

最后!!!好吧,有2个错误。 首先是我的路线       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);是在错误的地方,它必须在宣告意图之后。

第二个问题是我正在向我的ListActivity发送一个ArrayList并且试图接收一个String(错误的bundle方法)。就是这样了。

感谢所有想要帮助的人。这是服务的正确代码:

  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(Servicio.this.getBaseContext(),Lista.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Bundle b=new Bundle();
    b.putStringArrayList("imagenes",imagenes);
    i.putExtras(b);
    getApplication().startActivity(i);
} 

ListActivity

   public class Lista extends ListActivity {
private Bundle recogerDatos=new Bundle();
private ArrayList<String> imagenes;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    recogerDatos=this.getIntent().getExtras();

    imagenes=recogerDatos.getStringArrayList("imagenes");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, imagenes);
    setListAdapter(adapter);
}

答案 2 :(得分:0)

您在onStart()中运行此操作,这意味着this.getBaseContext()将返回null,因为在onCreate()之前不会创建上下文。

使用:

Intent i=new Intent(this.getApplicationContext(),Lista.class);

或将代码移至onCreate()