Android Studio error while passing array between Activity

Asked

Viewed 348 times

1

I’m developing an app that reads the barcode of an item and compares whether or not it exists in a list with database data. When any of these items is not present in the database it should save the data in an array and send them to another Activity where they will appear in a Spinner. When only one 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 of the two Acesvity:

Activity that reads and sends 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 the 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);
    }
}

Someone can help me What’s wrong?

  • If possible, indicate which part of the code, the cited error occurs.

  • I edited the question. The error happens when starting Activity, it does not open the second screen.

1 answer

3


Try it like this:

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

Fill in data from example:

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

etc....

Then run him through the Internet like this:

intent.putStringArrayListExtra("nEncontrados", nEncontrados);

And capture in the other Activity like this:

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

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

  • 1

    You want the first one added to be the first one on the list ? If so, yes, you can use it like this: list.get(position); Just be careful not to have "Array Index Out Of Bounds Exception"

  • 1

    thanks a lot, helped me a lot too :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.