1

I read this interesting post How to pass an object from one activity to another on Android and found that i should use putExtra to send object to other activities and to receive that using getIntent().getSerializableExtra("name") is necessary. but when i use putExtra it force me to cast the XMPPTCPConection object to (java.io.Serializable).

the java android code:

  XMPPTCPConnection connection;
  connection=  xmppConnectio.getXMPPConnectio();
  if(connection.equals(null))
  Log.d("","is  null");
  else
  Log.d("","is not null");

  Intent intent=  new Intent(this,chat.class);
  intent.putExtra("connection", (java.io.Serializable) connection);
  startActivity(intent);

but it throws exception

02-21 05:00:44.733    4672-4683/passargad.ehsan D/connected﹕ yes connected successfully :
02-21 05:00:44.733    4672-4672/passargad.ehsan D/﹕ is not null
02-21 05:00:44.733    4672-4672/passargad.ehsan D/AndroidRuntime﹕ Shutting down VM
02-21 05:00:44.733    4672-4672/passargad.ehsan W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x400205a0)
02-21 05:00:44.743    4672-4672/passargad.ehsan E/AndroidRuntime﹕ FATAL EXCEPTION: main
**java.lang.ClassCastException: org.jivesoftware.smack.tcp.XMPPTCPConnection**
at passargad.ehsan.MainActivity.triger(MainActivity.java:189)
at passargad.ehsan.XMPPConnectio$connectionXMPP.onProgressUpdate(XMPPConnectio.java:125)
at passargad.ehsan.XMPPConnectio$connectionXMPP.onProgressUpdate(XMPPConnectio.java:55)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432) at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4277)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

the interesting part of story is that it was ok for 3 times correct executing. but then it broke.

thank you in advance!

Community
  • 1
  • 1
Ehsan Jeihani
  • 1,238
  • 2
  • 14
  • 23

1 Answers1

4

The fact that you had to cast to Serializable should have told you the issue right there. I looked up the JavaDoc for XMPPTCPConnection and indeed, it does not implement Serializable.

Your app is dying at the point where you are trying to cast to Serializable, because the class doesn't implement it. The post you referenced mentioned "letting the custom class implement Serializable", and because it was a custom class, the developer could make that change. With a library class, you don't have that option.


You'll need to come up with a strategy to manage the connection, maybe with some kind of static reference (look at using WeakReference to avoid leaking the connection). You'll also have to handle the situation when the Activity is recreated (i.e. device rotation) and be able to re-establish the connection if/when you lose the reference to it.

I'm curious what the best solution would be for managing a connection like this. I don't have direct experience, but perhaps you can peruse some Smack implementations on GitHub to see how other developers have dealt with this issue.

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • thank you for your comment. but is there an other way letting me to pass the connection to other activity and getting that in onCreate – Ehsan Jeihani Feb 21 '16 at 12:24
  • 1
    Your best bet would be to take all the parameters for connection -- host, port, service name, etc. -- and put those in the intent for the new activity, which can then create the connection. – kris larson Feb 21 '16 at 13:41
  • the way you say means in every activity i should connect again. but i want to use the connection which is already connected. i really need this and i have done lots of solutions like serealization , bundle , global, but none of them can be used. – Ehsan Jeihani Feb 21 '16 at 14:36
  • i changed the scenario which i can send a class with Serializable implemented here is the question. [Parcelable encountered IOException when sending instance of an object to other activity](http://stackoverflow.com/questions/35538166/parcelable-encountered-ioexception-when-sending-inistance-of-an-object-to-other) thank you – Ehsan Jeihani Feb 21 '16 at 15:48