narco
narco

Reputation: 27

Simple manner pass arraylist between activity and intent

I am learning Android (I'm VERY newbie at the moment). I was looking and reading another posts but I have not find this exactly (or not simple). I want to pass a arraylist from an activity to a intent service, I think this is the simplest manner to do it; however I get NullPointer Exception.

public class MainActivity extends Activity {
private static final String TAG = "Main";
ArrayList<String> lista_actual = new ArrayList<String>();
...
    public void onClick (View v) {
    Intent msgIntent = new Intent(MainActivity.this, MiIntentService.class);
    lista_actual.add("probasndo cad");
    lista_actual.add("dfafsadf");
    lista_actual.add("dfasf");
    msgIntent.putStringArrayListExtra("lista", lista_actual);
    msgIntent.putExtra("iteraciones", 10);
    startService(msgIntent);
    //copiar();

}});

Then where I try get the array:

protected void onHandleIntent(Intent intent) 
{

ArrayList<String> lista_archivos = intent.getStringArrayListExtra("lista_actual");
Log.d ("Intent", Integer.toString(lista_archivos.size()));
.
.
.

Thanks.

Upvotes: 1

Views: 113

Answers (2)

ZealDeveloper
ZealDeveloper

Reputation: 783

While you are fetching your array list in intent service ur calling wrong key, it should be :

ArrayList<String> lista_archivos = intent.getStringArrayListExtra("lista");
Log.d ("Intent", Integer.toString(lista_archivos.size()));

Replace lista_actual with lista.

Upvotes: 1

NullPointerException
NullPointerException

Reputation: 4008

You need to serialize the arraylist in order to pass between activities. Further help will be found here. Hope that helps

Upvotes: 0

Related Questions