Search code examples
androidservicenullpointerexception

Service.startActivity(intent) throws null pointer exception


I have a weird problem that I can't solve. I'm trying to open a ListActivity from a Service I made of my own. The ListActivity receives foto paths from the service, which has to been show in a list. This is the code of the service:

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

When I Run this I always have a null pointer Exception.

I checked and nothing is null, not the bundle, not imagenes (which is an ArrayList<String> with 2 elements generated by accederFotosFolder()+crearArrayFotos() ). Neither is the intent. If I comment startActivity(i) everything works, so is that line for sure. First thing I thought was that mi ListActivity had something wrong, but it never goes into it's code, It just stops at startActivity(i) line.

Any suggestions? It's driving me mad.

UPDATE

 **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

Solution

  • SOLUTION!!!!!

    FINALLY!!! ok, there was 2 mistakes. First one is that my line i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); is in the wrong place, It have to be just after the declaration of the intent.

    Second problem is that i was sending an ArrayList to my ListActivity and triying to receive an String (wrong bundle method). So that's it.

    Thank's to everyone who wanted help. Here the right code of the service:

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

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