我无法收到我用意图发送的3个字符串数组

时间:2016-02-22 14:47:06

标签: android arrays android-intent extras

主要活动

     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);
        }
    });

另一个班级

public class claseinventario扩展了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:无法实例化活动 ComponentInfo {com.example.manuel.supercontrol / com.example.manuel.supercontrol.claseinventario}:java.lang.NullPointerException 02-22 14:52:58.975:E / AndroidRuntime(6623):引起:java.lang.NullPointerException

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

第15行#是 - Bundle extras = getIntent()。getExtras();

2 个答案:

答案 0 :(得分:0)

请改为尝试:

@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);
}

并检索它们:

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

为了使代码更清晰,您应该考虑使用标准的Java类命名约定(例如,大写的类名)和Extra名称的常量值,但上面的代码应该可以正常工作。

答案 1 :(得分:0)

请勿在Intent中使用getApplicationContext(),使用Activity.this

如果你传递的额外内容是arraylist,你可以直接传递arraylist

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

并且在其他活动中,您必须检查额外内容或特定键

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