0

I want this line of code to expect (String, Serializable) but it expects (String, boolean) instead.

intent.putExtra(CheckoutActivity.ZOOZ_INVOICE, invoice);

The error is:

The method putExtra(String, boolean) in the type intent is not applicable for the arguments (String, ZoozInvoice).

I want these arguments to have a (String, Serializable) signature instead of (String, boolean). It should be like this.

CheckoutActivity.ZOOZ_INVOICE = name

invoice = value

I have tried writing it like this:

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

This is accepted by Eclipse with no errors, and no compiling errors. However, the code does not work or do what it is supposed to.

In addition, I have tried to changing 'putExtra' to 'getIntExtra' instead and this error appears:

The method getIntExtra(String, int) in the type intent is not applicable for the arguments (String, ZoozInvoice).

I have also tried to change type of 'invoice' to 'boolean'. This results in:

Type mismatch: cannot convert ZoozInvoice to boolean.

And.

Cannotinvoke addItem (String, int, double, int, String, String) on the primitive type boolean.

How can I go about making the Intent expect (String, Serializable) instead of (String, boolean) ?

Code for reference and context.

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);
  • Are you sure your `ZoozInvoice`-class is implementing the `Serializable` interface? – Jave Aug 04 '13 at 17:14
  • Yes, I meant Serializable from the get go. The problem is still the same with Serializable. Can you guide me in the right direction, or help me come up with a solution? –  Aug 04 '13 at 17:22

2 Answers2

0

How can I go about making the Intent expect (String, Serialization) instead of (String, boolean) ?

You can't, because there is nothing in Android (or Java) named Serialization.

There is Serializable, and if you create a Java class that implements Serializable, you can use putExtra() with it. Parcelable would be a better choice for speed reasons, and not putting a ZoozInvoice in an extra at all is probably what I would be doing.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I meant Serializable. My problem persists when I choose (String, Serializable). I cannot use putExtra with it because of the reasons above. How would I go about using Parcelable? I have to put ZoozInvoice because I'm trying to integrate an in-app payment system. –  Aug 04 '13 at 17:22
  • @JustinBaxtron Let `ZooInvoice` implement `Parcelable`, e.g. http://stackoverflow.com/questions/7181526/example-of-implementing-parcelable – zapl Aug 04 '13 at 17:31
  • Okay, what if I don't want to use Parcellable though? What is wrong with my current code, and how I am using Serializable? My code is now in my original post for context and reference. –  Aug 04 '13 at 17:33
  • @JustinBaxtron: "I cannot use putExtra with it because of the reasons above" -- then you did not implement `Serializable`. You are welcome to edit your question and show the actual code for your `ZoozInvoice` class and your attempted `putExtra()` call. "How would I go about using Parcelable?" -- you [follow the instructions in the JavaDocs](http://developer.android.com/reference/android/os/Parcelable.html). "I have to put ZoozInvoice because I'm trying to integrate an in-app payment system" -- and your proof that you "have" to do this is, what, exactly? – CommonsWare Aug 04 '13 at 17:33
  • I have edited my question to show my actual code. I have to do it because I want an invoice for my products. I suppose using "have" is a wrong choice of words. But I would "like" to use Serializable and ZoozInvoice. I would like to do it this way without Parceable. My attempted putExtra() call is now in my original question. –  Aug 04 '13 at 17:35
  • @JustinBaxtron: Your problem would appear to be that you did not implement `Serializable` on `ZoozInvoice`. – CommonsWare Aug 04 '13 at 17:40
  • I did do that, though. I wrote out intent.putExtra. A list of suggestions pop up. I clicked on putExtra(String name, Serializable value). 'name' and 'value' then come up. And I change those to 'CheckoutActivity.ZOOZ_INVOICE' and 'invoice' respectively. What else do I have to do there? –  Aug 04 '13 at 17:45
  • @JustinBaxtron: You have to implement `Serializable` on `ZoozInvoice`. `Serializable` is a Java interface. http://www.javapractices.com/topic/TopicAction.do?Id=45 – CommonsWare Aug 04 '13 at 17:48
  • I'm a beginner and I'm not sure I understand all the way. But I'll read up on it. Thank you for your help. –  Aug 04 '13 at 17:54
  • I truly can't figure out how to implement Serializale on ZoozInvoice. Do I import it some how at the beginning of the code? –  Aug 04 '13 at 18:05
  • @JustinBaxtron: At minimum, type `implements Serializable` after `ZoozInvoice` in your class definition (`class ZoozInvoice implements Serializable`). You may also wish to read the JavaDoc for `Serializable` that I linked to in the answer, and the article I linked to in my preceding comment. You may also wish to pick up a book on Java development and spend a bit of time on the language itself, particularly the topics that I list here: http://commonsware.com/blog/2010/08/02/java-good-parts-version.html – CommonsWare Aug 04 '13 at 18:14
-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.
Mukesh Garg
  • 711
  • 1
  • 6
  • 20
  • I posted my code in my original question. I'm just not following you exactly. Is there a way to state what you're saying in the context of my code? –  Aug 04 '13 at 18:13