死机是由于通过上下文序列化类 [英] crash due to passing context to a serializable class

查看:140
本文介绍了死机是由于通过上下文序列化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/12408386/serializable-crash-error-when-loading-web-broser-from-my-application\">Serializable崩溃错误加载从我的应用程序网络broser时

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

低于code导致飞机坠毁时,我试图通过按钮点击界面

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;
    }

}

现在,如果我从发现的构造函数中删除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.

如何解决这个问题,而不从发现的构造去除MainTest类类型?

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

推荐答案

这个问题是由于创建其相关对象时,主要活动传递给序列化类。在我们的例子传递 MainTest 对象的搜索创建时,它的对象。
解决方案:创建一个包含所有发现数据的另一个序列化的类(数据是serialzable)

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).

这篇关于死机是由于通过上下文序列化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆