1

Possible Duplicate:
Serializable crash error when loading web broser from my application

below a code causing a crash when I try to open a web browser from my application by a click on the button on the interface

MainTest.java

package com.heeere.androiddnssd.discovery;

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;

public class MainTest extends Activity {

    private Discovery discovery; 

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

    @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);
        Button b = (Button)this.findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
                startActivity(browserIntent);
            }
        });
    }

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

Discovery.java

package com.heeere.androiddnssd.discovery;

import java.io.Serializable;

public class Discovery implements Serializable {


    private static final long serialVersionUID = 637576886455091135L;
    private MainTest maintest;
    //android.os.Handler handler = new android.os.Handler();

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

}

Now, If I remove the MainTest Class type from the constructor of Discovery, the bug will disappear. So that's the cause of the crash.

How to fix this problem without removing the MainTest Class type from the constructor of Discovery?

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

1 Answers1

0

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