Parcelable 在将对象的实例发送到其他活动时遇到 IOException [英] Parcelable encountered IOException when sending instance of an object to other activity

查看:37
本文介绍了Parcelable 在将对象的实例发送到其他活动时遇到 IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 activit-main 的 onCreate 中,我创建了一个名为 XMPPConectio 的类的实例在 XMMPPConnection 的构造函数中,我对活动主进行了实例化,因此我执行了一个 Asychtask.在progressUpdate 中,我调用activity-main 的触发函数.我想通过intent.putExtra将XMPPconnectio的实例发送到其他活动,但我收到错误

in the onCreate of activit-main i make an inistance of a class named XMPPConectio at the constructor of the XMMPPConnection i take inistance of the activity-main and therefor i execute an Asychtask. in the progressUpdate i call triger function of activity-main. i the triger function i want to send inistance of XMPPconnectio to other activity by intent.putExtra but i get the error

Parcelable 在写入可序列化对象时遇到 IOException

Parcelable encountered IOException writing serializable object

执行所有这些操作的目的是与其他活动建立连接(当它连接时).

the aim of doing all these is to have connection (when it is connected) to other activity.

请给我一些示例代码谢谢

please give me some sample code thank you

实现可序列化的 XMPPConectio 类:

public class XMPPConnectio implements Serializable {


    XMPPTCPConnection connection;
    String connectionMessages="";
    connectionXMPP connectionXMPPAsynch;
    MainActivity mainActivity;


    public XMPPTCPConnection getXMPPConnectio ()
    {
        return connection;
    }

   public XMPPConnectio(MainActivity mainActivity)
    {
    this.mainActivity=mainActivity;
    try
    {
        connectionXMPPAsynch =new connectionXMPP();
        connectionXMPPAsynch.execute();
    }
    catch (Exception e)
    {
    }
    }

    class connectionXMPP extends AsyncTask<String,Void,String>
    {

        @Override
        protected String doInBackground(String... params) {


            connection = new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder()
            .setServiceName("192.168.1.6")
            .setUsernameAndPassword("ehsan", "123")
            .setPort(9090)
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .setDebuggerEnabled(true)
            .setCompressionEnabled(false).build());
            connection.setUseStreamManagement(true);
            connection.addConnectionListener(new ConnectionListener()
            {

                @Override
                public void connected(XMPPConnection connection) {
                    Log.d("connected", "yes connected successfully : ");
                    publishProgress();
                }

                @Override
                public void authenticated(XMPPConnection connection, boolean resumed) {
                    Log.d("connected","yes authenticated successfully : ");
                }

                @Override
                public void connectionClosed() {
                    Log.d("connected","yes connectionClosed successfully : ");
                }

                @Override
                public void connectionClosedOnError(Exception e) {

                    Log.d("connected","yes connectionClosedOnError  : ");
                }

                @Override
                public void reconnectionSuccessful() {
                    Log.d("connected","yes reconnection successfully : ");
                }

                @Override
                public void reconnectingIn(int seconds) {
                    Log.d("connected","yes reconnectingIn  : ");
                }

                @Override
                public void reconnectionFailed(Exception e) {
                    Log.d("connected","yes reconnectionFailed  : ");
                }
            });

           connect();
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            mainActivity.triger();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.d("","onPostExecute");
        }
         private void connect()
        {
        try {

                connection.connect();

            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMPPException e) {
                e.printStackTrace();
            }
        }
    }
}

创建XMPPConectio实例并导致Asychtask执行的Activity

package passargad.ehsan;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.Socket;

public class MainActivity extends ActionBarActivity implements Serializable {


    private Socket socket;
    private String serverIpAddress = "192.168.1.6";
    XMPPConnectio xmppConnectio;
    XMPPTCPConnection connection;
    private static final int REDIRECTED_SERVERPORT = 6789;


    FastFood fastFood;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=new Intent(this,SocketService.class);
        startService(intent);

        fastFood =new FastFood(this);

        xmppConnectio=new XMPPConnectio(this);


    }

// this is the function which is called (when connection is done ) by //onProgressUpdate of Asychtask 

   public void triger()
    {


        try {
            Intent intent=  new Intent(this,chat.class);
            intent.putExtra("XMPP",xmppConnectio);
             startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}

这是我想要在其中安装 XMPPConnectio 的活动,但执行从未达到此

package passargad.ehsan;

import android.app.Application;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;

import java.io.IOException;


public class chat extends ActionBarActivity {
    XMPPConnectio xmppConnectio;
    XMPPTCPConnection connection;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);


        xmppConnectio=(XMPPConnectio)getIntent().getSerializableExtra("XMPP");
        Log.d("","done");
 }
}

我试图通过提供所有代码来使问题变得清晰.正如我所说,目标是在连接连接时在所有活动中建立连接.谢谢.

i tried to make the question crystal clear by giving all the code. as i said the goal is to have the connection in all activities when connection is connected. thank you .

推荐答案

如果我想在每个活动中都可以访问连接对象,其中一种解决方案是使用 bundle 并有意发送 bundle.但实际问题是在 API 级别 10 (android 2.3) 中没有捆绑选项.另一个问题是,无法选择使用序列化和 putExtra 来实现意图,因为 XMPPTCPConnection 不实现可序列化.所以我使用了定义静态对象的第三个选项.

if i want to have connection object accessible in every activities one of the solutions is using bundle and send the bundle with intent. but the actual problem is not having bundle option in API level 10 (android 2.3). the other problem was ,not having the option to use serializing and putExtra for intent because XMPPTCPConnection does not implemet serializable. so i used the third option which was defining the object static.

所以我将 XMPPConectio 类更改为这个

 import android.app.Application;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.IBinder;
    import android.util.Log;

    import org.jivesoftware.smack.ConnectionConfiguration;
    import org.jivesoftware.smack.ConnectionListener;
    import org.jivesoftware.smack.SmackException;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.chat.Chat;
    import org.jivesoftware.smack.chat.ChatManager;
    import org.jivesoftware.smack.chat.ChatManagerListener;
    import org.jivesoftware.smack.chat.ChatMessageListener;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smack.tcp.XMPPTCPConnection;
    import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

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


    public class XMPPConnectio  {


        XMPPTCPConnection connection;
        String connectionMessages="";
        connectionXMPP connectionXMPPAsynch;
        MainActivity mainActivity;


        public XMPPTCPConnection getXMPPConnectio ()
        {
            return connection;
        }

       public XMPPConnectio(MainActivity mainActivity)
        {
        this.mainActivity=mainActivity;
        try
        {
            connectionXMPPAsynch =new connectionXMPP();
            connectionXMPPAsynch.execute();
        }
        catch (Exception e)
        {
        }
        }

        class connectionXMPP extends AsyncTask<String,Void,String>
        {

            @Override
            protected String doInBackground(String... params) {


                connection = new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder()
                .setServiceName("192.168.1.6")
                .setUsernameAndPassword("ehsan", "123")
                .setPort(9090)
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setDebuggerEnabled(true)
                .setCompressionEnabled(false).build());
                connection.setUseStreamManagement(true);
                connection.addConnectionListener(new ConnectionListener()
                {

                    @Override
                    public void connected(XMPPConnection connection) {

                        Log.d("connected","yes connected successfully : ");
                        publishProgress();
                    }

                    @Override
                    public void authenticated(XMPPConnection connection, boolean resumed) {
                        Log.d("connected","yes authenticated successfully : ");

                    }

                    @Override
                    public void connectionClosed() {
                        Log.d("connected","yes connectionClosed successfully : ");
                    }

                    @Override
                    public void connectionClosedOnError(Exception e) {

                        Log.d("connected","yes connectionClosedOnError  : ");
                    }

                    @Override
                    public void reconnectionSuccessful() {
                        Log.d("connected","yes reconnection successfully : ");
                    }

                    @Override
                    public void reconnectingIn(int seconds) {
                        Log.d("connected","yes reconnectingIn  : ");
                    }

                    @Override
                    public void reconnectionFailed(Exception e) {
                        Log.d("connected","yes reconnectionFailed  : ");
                    }
                });

               connect();
                return null;
            }

            @Override
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
                mainActivity.triger();
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Log.d("","onPostExecute");
            }
             private void connect()
            {
            try {

                    connection.connect();
                   // connection.login();

                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
            }
        }
    }

创建XMPPConectio实例并导致Asychtask执行的Activity

package passargad.ehsan;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.Socket;

public class MainActivity extends ActionBarActivity {

private Socket socket;
private String serverIpAddress = "192.168.1.6";
XMPPConnectio xmppConnectio;
public static XMPPTCPConnection connection;
private static final int REDIRECTED_SERVERPORT = 6789;


FastFood fastFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent=new Intent(this,SocketService.class);
    startService(intent);
    fastFood =new FastFood(this);

    //this is
    xmppConnectio=new XMPPConnectio(this);



}

public void triger()
{

    connection=xmppConnectio.getXMPPConnectio();
    Intent intent=  new Intent(this,chat.class);
    startActivity(intent);

}

}

这是我们可以在那里访问连接的活动

package passargad.ehsan;

import android.app.Application;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;

import java.io.IOException;


public class chat extends ActionBarActivity {
XMPPTCPConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);

    //this is
        connection=MainActivity.connection;
        connection.addConnectionListener(new ConnectionListener() {
            @Override
            public void connected(XMPPConnection connection) {
                Log.d("","connected");

            }
        @Override
        public void authenticated(XMPPConnection connection, boolean resumed) {
            Log.d("","6");
        }

        @Override
        public void connectionClosed() {
            Log.d("","5");
        }

        @Override
        public void connectionClosedOnError(Exception e) {
            Log.d("","4");
        }

        @Override
        public void reconnectionSuccessful() {
            Log.d("","3");
        }

        @Override
        public void reconnectingIn(int seconds) {
            Log.d("","2");
        }

        @Override
        public void reconnectionFailed(Exception e) {
            Log.d("","1");
        }
    });
    try {
        connection.login();
    } catch (XMPPException e) {
        e.printStackTrace();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}
}

这篇关于Parcelable 在将对象的实例发送到其他活动时遇到 IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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