使用intent

时间:2016-06-17 05:50:33

标签: android android-intent arraylist

public class Checkout extends AppCompatActivity {

    //ArrayList<String> stringArray = new ArrayList<>();
    ArrayList<Integer> ekdum_final_order_names = new ArrayList<>();
    Intent intent = getIntent();

     public ArrayList<Integer> intArray_id_selected;
    public ArrayList<Integer> intArray_no;
    Map<Integer ,String> linkedHashMap = new LinkedHashMap<>();

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

        intArray_no = (ArrayList<Integer>) intent.getSerializableExtra("int_array_no");
        intArray_id_selected = (ArrayList<Integer>) intent.getSerializableExtra("int_array_id");
        Toast.makeText(getBaseContext(),intArray_id_selected.get(0)+"  "+ intArray_no.get(0),Toast.LENGTH_SHORT).show();
    }

这是使用intent从antoher类接收arraylists的类。我得到一个未经检查的强制警告:"Unchecked cast: 'java.io.Serializable' to 'java.util.ArrayList<android.app.Fragment>'" and due to this i get java null pointer exception.这是logcat:

  

引起:java.lang.NullPointerException:尝试调用虚拟   方法&#39; java.io.Serializable   android.content.Intent.getSerializableExtra(java.lang.String中)&#39;在...上   null对象引用                                                                                          在   com.example.gandhjee.pantry_order.Checkout.onCreate(Checkout.java:39)

请帮我找出错误。任何帮助将受到高度赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

您应该在getIntent()内拨打onCreate()。 由于Intent在onCreate()之后可用,Intent之前会出现onCreate()

试试这个:

public class Checkout extends AppCompatActivity {

    //ArrayList<String> stringArray = new ArrayList<>();
    ArrayList<Integer> ekdum_final_order_names = new ArrayList<>();

    public ArrayList<Integer> intArray_id_selected;
    public ArrayList<Integer> intArray_no;
    Map<Integer ,String> linkedHashMap = new LinkedHashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkout);
        Intent intent = getIntent();
        intArray_no = (ArrayList<Integer>) intent.getSerializableExtra("int_array_no");
        intArray_id_selected = (ArrayList<Integer>) intent.getSerializableExtra("int_array_id");
        Toast.makeText(getBaseContext(),intArray_id_selected.get(0)+"  "+ intArray_no.get(0),Toast.LENGTH_SHORT).show();
    }

如果这不起作用,请尝试创建阵列并使用目标数据克隆它:

intArray_no = new ArrayList<Integer>((Integer)intent.getSerializableExtra("int_array_no");