Android Studio error passing array between activity

2

I'm developing an application that reads an item's barcode and compares whether it exists in a list with bank data. When one of these items is not present in the database, it must write the data to an array and send it to another activity where it will appear in a Spinner. When only a code needs to be sent it works ok. However, when I try to send two or more, the following error appears when trying to start the second activity:

  

E / AndroidRuntime: FATAL EXCEPTION: main                     Process: com.weebly.wlhtech.confitem, PID: 24955                     java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString ()' on a   null object reference

Follow the code for the two activities:

Activity that reads and submits data:

public void compareData(String patrimonio) {
        for(i=0;i<(lstItens.getAdapter().getCount());i++) {
            Log.d("String ", lstItens.getItemAtPosition(i).toString());
            if(patrimonio.contentEquals(lstItens.getItemAtPosition(i).toString())) {
                Log.d("Resultado: igual", lstItens.getItemAtPosition(i).toString());
                ok = true;
                break;
            } else {
                Log.d("Resultado: diferente", patrimonio);
            }
        }
        if(ok == true) {
            Log.d("Resultado encontrado", String.valueOf(i));
        } else {
            Toast.makeText(this, "Patrimônio não encontrado.", Toast.LENGTH_SHORT).show();
            naoencontrados = new String[100];
            naoencontrados[posicao] = patrimonio;
            posicao +=1;
        }

        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setTitle("Continuar conferência");

        builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                camera();
            }
        });
        builder.setNeutralButton("Finalizar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                scannerView.stopCameraPreview();
                Intent confSetorResult = new Intent(ListaConf.this, confSetorResult.class);
                confSetorResult.putExtra("nEncontrados", naoencontrados);
                confSetorResult.putExtra("setor", setor);
                startActivity(confSetorResult);

            }
        });
        builder.setMessage("Deseja adicionar mais algum item?");
        android.app.AlertDialog alert = builder.create();
        alert.show();

Activity that receives data:

package com.weebly.wlhtech.confitem;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class confSetorResult extends AppCompatActivity {

    Spinner spnNEncontrados;
    String setor;
    ArrayAdapter<String> adapter;

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

        spnNEncontrados = findViewById(R.id.spnNEncontrados);

        //Pegar dados da outra Activity
        Intent intent = getIntent();
        String[] nEncontrados = intent.getStringArrayExtra("nEncontrados");
        setor = intent.getStringExtra("setor");

        //Popular Spinner
        if(nEncontrados == null) {
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
            spnNEncontrados.setAdapter(adapter);
        } else {
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nEncontrados);

            spnNEncontrados.setAdapter(adapter);
        }

    }

    public void alterar(View v) {
        Intent altera = new Intent(this, AlteraSetor.class);
        altera.putExtra("Setor", setor);
        altera.putExtra("Patrimonio", spnNEncontrados.getSelectedItem().toString());
        altera.putExtra("Nome", 0);
        startActivity(altera);
    }
}

Can anyone help me with what's wrong?

    
asked by anonymous 18.05.2018 / 00:04

1 answer

3

Try this:

 ArrayList<String> nEncontrados = new ArrayList<>();

Fill in the data of the example nFind:

lista.add("1");
lista.add("2");

etc ...

Then pass it on by trying like this:

intent.putStringArrayListExtra("nEncontrados", nEncontrados);

And capture in another Activity like this:

 Bundle bundle = getIntent().getExtras();
 List<String> lista = bundle.getStringArrayList("nEncontrados");

It's interesting to note that it was using a List and not a vector, as you are using.

    
18.05.2018 / 01:54