-2

I have MainActivity: public class MainActivity extends Activity {

private ArrayList<Antrenament> listaAntr = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Antrenament a1 = new Antrenament("forta","12/06/2019",50,"Andrei");
    listaAntr.add(a1);

    Button bAdd = (Button) findViewById(R.id.buttonAdd);
    Button bLista = (Button) findViewById(R.id.buttonList);


    bAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), AdaugareAntrenament.class);
            intent.putParcelableArrayListExtra("listaant",listaAntr);
            startActivityForResult(intent,1);

        }
    });


    bLista.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), ListaAntr.class);
            intent.putParcelableArrayListExtra("listaant",listaAntr);
            startActivityForResult(intent,1);
        }
    });


    Intent intent2 = getIntent();
    listaAntr = intent2.getParcelableArrayListExtra("listaantr");


}

}

I want to send the list of objects Antrenament name listaAntr to AdaugareAntrenament activity, where I want to create a new Antrenament object, add it to the list and then send back the list to mainactivity. This updated list I want to send afterward to ListaAntr Activity, from mainactivity.

AdaugareAntrenament activity:

public class AdaugareAntrenament extends Activity {

 ArrayList<Antrenament> listaAntr1=new ArrayList<>();
//private Button add;
 EditText etcateg, etdurata, etinstr,etdata;
List<String> listacateg=new ArrayList<>();
Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_adaugare_antrenament);

    Intent intent = getIntent();
   listaAntr1 = intent.getParcelableArrayListExtra("listaant");

    Button add = findViewById(R.id.buttonSave);
    etdurata=findViewById(R.id.editTextDurata);
    etinstr=findViewById(R.id.editTextInstructor);
    etdata=findViewById(R.id.editTextData);
    spinner=findViewById(R.id.spinner);

    listacateg=new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.categ)));

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,listacateg);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String categorie,instructor,data;
            int durata;

            durata = Integer.parseInt(etdurata.getText().toString());
            instructor = etinstr.getText().toString().trim();
            data = etdata.getText().toString().trim();
            categorie = spinner.getSelectedItem().toString().trim();



            if(TextUtils.isEmpty(instructor)) {
                etinstr.setError("Introduceti numele instructorului");
                return;
            }

            if(TextUtils.isEmpty(data)) {
                etdata.setError("Introduceti data programarii");
                return;
            }

                Antrenament antr = new Antrenament(categorie, data, durata, instructor);
                listaAntr1.add(antr);
                Intent intent=new Intent(getApplicationContext(), MainActivity.class);
                intent.putParcelableArrayListExtra("listaantr",listaAntr1);

        }
    });

}

For the moment I get that error when I try to create the Antrenament object.. but this after some changes, because my code doesn't even send the updated list back to main activity.

Current error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
        at com.example.ancaa.rezolvarerestanta20181.AdaugareAntrenament$1.onClick(AdaugareAntrenament.java:78)

The error points to the line :

  listaAntr1.add(antr);

and ListaAntr Activity:

public class ListaAntr extends Activity {

public ArrayList<Antrenament> listaAntr = new ArrayList<>();
public ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_antr);

    lv = (ListView)findViewById(R.id.lista);

    Intent intent = getIntent();
    listaAntr = intent.getParcelableArrayListExtra("listaantr");

    ArrayAdapter<Antrenament> adapter=new ArrayAdapter<Antrenament>(this,android.R.layout.simple_list_item_1,listaAntr);
    lv.setAdapter(adapter);

}

}

Here the content of the arraylist should be shown as a listview.

Onix
  • 3
  • 3

1 Answers1

0

getParcelableArrayListExtra(String); isn't finding the Array in the intent and returning a null. Look at the documentation for the function.

From there it looks like you are doing something wrong in MainActivity.java. You are overwriting the information in listaAntr in the oncreate with this line listaAntr = intent2.getParcelableArrayListExtra("listaantr"); and then passing that newly nulled Array to the next Activity when you click, thus causing your issue.

Mike D.
  • 3
  • 1