1

I'm developing an application.

This application contains 2 classes

  • Discovery class which implements serializable

  • Main class which extends Activity

In the Main class I call the android web browser on a click on the button in the application interface . And this behaviour generate a crash.

bellow is the code:

MainTest.java

package com.heeere.androiddnssd.discovery;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainTest extends Activity {

    android.net.wifi.WifiManager.MulticastLock lock;
    private Discovery discovery; 
    private TextView textView;

    /** Called when the activity is first created. */

    @SuppressLint("NewApi") @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ( savedInstanceState == null)
            discovery = new Discovery(this);
        else
            discovery = (Discovery) savedInstanceState.getSerializable("discovery");
        setContentView(R.layout.main);
        textView = (TextView)this.findViewById(R.id.text);
        Button b = (Button)this.findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //discovery.setUp();
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
                startActivity(browserIntent);
            }
        });

        android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
        lock = wifi.createMulticastLock("mylockthereturn");
        lock.setReferenceCounted(true);
        lock.acquire();

    }

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        outState.putSerializable("discovery", discovery);
        super.onSaveInstanceState(outState);
    }

    public void updateView () {
        String msg = discovery.getMsg();
        textView.setText(msg);
    }


    @SuppressLint("NewApi") @Override
    protected void onStop() {
        discovery.stop();
        lock.release();
        super.onStop();
    }


}

Discovery.java

package com.heeere.androiddnssd.discovery;

import java.io.IOException;
import java.io.Serializable;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;

public class Discovery implements Serializable {


    private static final long serialVersionUID = 637576886455091135L;
    private String type = "_ikunet._tcp.local.";
    private String msg="";
    private JmDNS jmdns = null;
    private ServiceListener listener = null;
    private MainTest maintest;
    android.os.Handler handler = new android.os.Handler();

    public Discovery (MainTest maintest) {
        this.maintest = maintest;
        setUp();
    }

    public void setUp() {

        try {
            jmdns = JmDNS.create();
            jmdns.addServiceListener(type, listener = new ServiceListener() {

                public void serviceResolved(ServiceEvent ev) {
                    msg = msg + ev.getInfo().getName()+ "\n";
                    update();
                }

                public void serviceRemoved(ServiceEvent ev) {
                }

                public void serviceAdded(ServiceEvent event) {
                    jmdns.requestServiceInfo(event.getType(), event.getName(), 1);
                }
            });
        } catch (IOException e) {
            //e.printStackTrace();
            return;
        }
    }

    public String getMsg() {
        return msg;
    }

    private void update() {
        handler.postDelayed(new Runnable() {
            public void run() {
                maintest.updateView();
            }
        }, 1);
    }


    public void stop() {
        if (jmdns != null) {
            if (listener != null) {
                jmdns.removeServiceListener(type, listener);
                listener = null;
            }
            jmdns.unregisterAllServices();
            try {
                jmdns.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            jmdns = null;
        }
    }

}

LogCat

09-13 15:08:30.507: E/AndroidRuntime(15775): Uncaught handler: thread main exiting due to uncaught exception
09-13 15:08:30.517: E/AndroidRuntime(15775): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.heeere.androiddnssd.discovery.Discovery)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.os.Parcel.writeSerializable(Parcel.java:1131)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.os.Parcel.writeValue(Parcel.java:1085)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.os.Parcel.writeMapInternal(Parcel.java:469)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.os.Bundle.writeToParcel(Bundle.java:1445)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.os.Parcel.writeBundle(Parcel.java:483)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:1427)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3106)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.app.ActivityThread.access$2400(ActivityThread.java:119)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.os.Looper.loop(Looper.java:123)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.app.ActivityThread.main(ActivityThread.java:4363)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.lang.reflect.Method.invokeNative(Native Method)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.lang.reflect.Method.invoke(Method.java:521)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at dalvik.system.NativeStart.main(Native Method)
09-13 15:08:30.517: E/AndroidRuntime(15775): Caused by: java.io.NotSerializableException: android.os.Handler
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1547)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
09-13 15:08:30.517: E/AndroidRuntime(15775):    at android.os.Parcel.writeSerializable(Parcel.java:1126)
09-13 15:08:30.517: E/AndroidRuntime(15775):    ... 16 more
Iulia Barbu
  • 1,522
  • 12
  • 25
MOHAMED
  • 41,599
  • 58
  • 163
  • 268

3 Answers3

1

the problem is due to pass main activity to serializable class when creating its related object. in our case passing the MainTest Object to the Discovery object when creating it. The solution: create another serializable class which contains all Discovery data (data to be serialzable).

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
-1

The error is pretty clear: Caused by: java.io.NotSerializableException: android.os.Handler

Figure out how to take the handler out: android.os.Handler handler = new android.os.Handler();

dmon
  • 30,048
  • 8
  • 87
  • 96
  • You should probably create another class that has just your data, without any extra logic. – dmon Sep 13 '12 at 14:23
  • I m new be in android and java. is it possible to be more specific or give me an example – MOHAMED Sep 13 '12 at 14:33
  • the crash cause is not the handler. please refer to this link to see the rel problem: http://stackoverflow.com/questions/12411012/crash-due-to-passing-context-to-a-serializable-class – MOHAMED Sep 14 '12 at 07:20
  • Errr... of course! You can't serialize an Activity either! – dmon Sep 17 '12 at 02:14
-1

As far as I know Serializable is slow on Android, you should use Parcelable instead, see this link for more info: Benefit of using Parcelable instead of serializing object

Regards

Community
  • 1
  • 1
Spike777
  • 227
  • 5
  • 12