如何使Intent期望(String,Serializable)而不是(String,boolean)?

时间:2013-08-04 17:03:56

标签: java android android-intent

我想要这行代码(String,Serializable),但它需要(String,boolean)。

intent.putExtra(CheckoutActivity.ZOOZ_INVOICE, invoice);

错误是:

方法类型putExtra(String,boolean)方法不适用于参数(String,ZoozInvoice)。

我希望这些参数具有(String,Serializable)签名而不是(String,boolean)。它应该是这样的。

CheckoutActivity.ZOOZ_INVOICE =姓名

invoice = value

我试过这样写:

intent.putExtra(CheckoutActivity.ZOOZ_INVOICE, invoice(CheckoutActivity.ZOOZ_INVOICE, invoice));

这是Eclipse接受的,没有错误,也没有编译错误。但是,代码不起作用或按预期执行。

此外,我尝试将'putExtra'更改为'getIntExtra'而出现此错误:

类型为intent的getIntExtra(String,int)方法不适用于参数(String,ZoozInvoice)。

我还尝试将'invoice'的类型更改为'boolean'。这导致:

类型不匹配:无法将ZoozInvoice转换为布尔值。

在基本类型boolean上禁止addItem(String,int,double,int,String,String)。

我怎样才能使Intent期望(String,Serializable)而不是(String,boolean)?

参考和背景代码。

ZooZInvoice invoice = new ZooZInvoice();
        invoice.addItem("item1", 1, 0.5, 0, "1", "good choice!");
        invoice.addItem("item2", 1, 3, 0, "2", "additional details for item 2");
        invoice.addItem("item3", 1, 2, 0, "3", "additional details for item 3");
        invoice.addItem("item4", 1, 8, 0, "4", "additional details for item 4");
        invoice.setInvoiceNumber("5512-FA");
        invoice.setInvoiceAdditionalDetails("Power Ups for user 12345");
        intent.putExtra(CheckoutActivity.ZOOZ_INVOICE, invoice);

        startActivityForResult(intent, ZooZ_Activity_ID);

2 个答案:

答案 0 :(得分:0)

  

我怎样才能使Intent期望(String,Serialization)而不是(String,boolean)?

你不能,因为Android(或Java)中没有任何名为Serialization的内容。

Serializable,如果您创建实现Serializable的Java类,则可以use putExtra() with it。出于速度原因,Parcelable会是更好的选择,而且根本不会将ZoozInvoice添加到额外内容中,这可能就是我要做的事情。

答案 1 :(得分:-1)

//This is the code from parent Activity which is calling the Intent to happen.


Intent intent = new Intent(this, PassToAnotherActivity.class);
//intent.putExtra("Serializable Key", serializable object);

Employee e=new Employee();
e.setName("Brijesh");
emp.add(e);

intent.putExtra("Employee", emp);

startActivity(i);


// This is the code to accept serializable object/Data in the Called Activity

PassToAnotherActivity.java


public void onCreate(Bundle savedInstanceState)
{

     Bundle bundle = getIntent().getExtras();
     Employee emp = (Employee) bundle.getSerializable("Employee");
     // Display the object
}



I hope this will help u.