I can't receive 3 Arrays of Strings that I sent with an Intent

61 Views Asked by At

Main activity

     public void onClick(View v) {
            String[] arraynomb = arreglonombreprod.toArray(new String[0]);
            String[] arrayprecios = precioproductoarreglo.toArray(new String[0]);
            String[] arraycant = arreglocantidadprod.toArray(new String[0]);
            Bundle extras = new Bundle();
            Intent intent = new Intent(getApplicationContext(),claseinventario.class);
            extras.putStringArray("productosx", arraynomb);
            extras.putStringArray("preciosx", arrayprecios);
            extras.putStringArray("cantidadesx", arraycant);
            intent.putExtras(extras);
            startActivity(intent);
        }
    });

the other class

public class claseinventario extends Activity {

ListViewAdapter adapter;


Bundle extras = getIntent().getExtras();
String[] prod = extras.getStringArray("productosx");
String[] cant = extras.getStringArray("preciosx");
String[] pre = extras.getStringArray("cantidadesx");



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lista);
    final ListView lista = (ListView) findViewById(R.id.viewlista);
    adapter = new ListViewAdapter(this, prod, pre, cant);
    lista.setAdapter(adapter);


}

logcat:

02-22 14:52:58.975: E/AndroidRuntime(6623): FATAL EXCEPTION: main

02-22 14:52:58.975: E/AndroidRuntime(6623): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.manuel.supercontrol/com.example.manuel.supercontrol.claseinventario}: java.lang.NullPointerException 02-22 14:52:58.975: E/AndroidRuntime(6623): Caused by: java.lang.NullPointerException

02-22 14:52:58.975: E/AndroidRuntime(6623): at com.example.manuel.supercontrol.claseinventario.(claseinventario.java:15)

line 15# is - Bundle extras = getIntent().getExtras();

2

There are 2 best solutions below

0
On

Try this instead:

@Override
public void onClick(View v) {
    String[] arraynomb = arreglonombreprod.toArray(new String[0]);
    String[] arrayprecios = precioproductoarreglo.toArray(new String[0]);
    String[] arraycant = arreglocantidadprod.toArray(new String[0]);
    Intent intent = new Intent(getApplicationContext(),claseinventario.class);
    intent.putExtra("productosx", arraynomb);
    intent.putExtra("preciosx", arrayprecios);
    intent.putExtra("cantidadesx", arraycant);
    startActivity(intent);
}

And to retrieve them:

Intent intent = getIntent();
String[] prod = intent.getExtra("productosx");
String[] cant = intent.getExtra("preciosx");
String[] pre = intent.getExtra("cantidadesx");

To make the code even cleaner, you should consider using standard Java class naming conventions (uppercase Class names, for example), and constant values for the Extra names, but the code above should work fine.

0
On

Don't use getApplicationContext() in Intent,Use Activity.this

if the extras you are passing are arraylist,you can directly pass the arraylist as

intent.putStringArrayListExtra("key",arraylist here);

and in the other activity you have to check for nullpointer on extras or the specific key

Bundle bundle = getIntent().getExtras();
if(bundle != null){
  if(bundle.containsKey("key)){
    //....extract extra here
  }
}