Android:使用 Parcel 将对象从一个活动传递到另一个活动时遇到困难

发布于 2024-12-12 10:03:58 字数 12421 浏览 0 评论 0原文

我读过很多关于如何使用 Parcel 的文章,并且在我的应用程序中使用过它,但它不起作用。我刚刚创建了一个活动,您可以在其中使用Sockets 连接到计算机。我已经在套接字的 OutputStream 上打开了一个 java.io.PrintWriter 并且我想要另一个活动(在触摸第一个活动中的按钮时打开)使用它相同的PrintWriter。我认为我在这里犯的错误是没有克隆 PrintWriter 对象。我不知道如何克隆它。

这是第一个 Activity(我删除了一些不需要的函数):

public class TestActivity extends Activity implements OnClickListener
{
    String ip;
    int port;
    Socket soc;
    PrintWriter writer;   // this works fine here

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        ip = "";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Connect button click handler (you can skip this part)
        Button connect = (Button)findViewById(R.id.connectBTN);
        connect.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                EditText TB = (EditText)findViewById(R.id.ipTB);
                ip = TB.getText().toString();
                TB = (EditText)findViewById(R.id.portTB);
                port = Integer.parseInt(TB.getText().toString());
                try {
                soc = new Socket(ip,port);
                showDialog("Connected successfully!");
                writer = new PrintWriter(soc.getOutputStream(), true);
                }
                catch(Exception er)
                {
                    showDialog("Couldn't connect.\n"+er.getMessage());
                    soc = null;
                }
            }
        } );

        findViewById(R.id.controlmouseBTN).setOnClickListener(this);

        // disconnect click handler  (skip this part too)
        Button disconnectBTN = (Button)findViewById(R.id.disconnectBTN);
        disconnectBTN.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                try
                {
                    if(soc.isConnected()) soc.close();
                    writer.close();
                }
                catch(Exception er) { showDialog("Couldn't close connection.\n"+er.getMessage()); }
            }
        });

    }

    @Override
    public void onClick(View v)   // here is the erroneous part 
    {
        //if(soc == null || !soc.isConnected()) { showDialog("Not connected to any computer."); return; } 
        //if(writer == null) return;
        Intent i = new Intent(getApplicationContext(), PCController.class);
        Intermediate inter = new Intermediate();   // this is the Parcelable class I've created
        //inter.set(new String("hello"));  (I tried sending a string, which worked fine, just to confirm that my Parcelable class is working)
        inter.set(writer);
        i.putExtra("printer", inter);
        startActivity(i);
    }

}

这是 Parcelable 类 Intermediate 的代码

import android.os.*;

public class Intermediate implements Parcelable
{
    Object ob;

    public Intermediate()
    {}

    public Intermediate(Parcel parcel)
    {
        this.ob = parcel.readValue(Intermediate.class.getClassLoader());
    }

    public void set(Object o)
    { this.ob = o; }

    public Object get()
    {
        return ob;
    }

    @Override
    public int describeContents()
    { return 0; }

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
        dest.writeValue(this.ob);
    }

   // I don't understand this part though! Just copy pasted and modified the ClassName

    public static final Intermediate.Creator<Intermediate> CREATOR = new Intermediate.Creator<Intermediate>() {
        public Intermediate createFromParcel(Parcel in) {
            return new Intermediate(in); }

        public Intermediate[] newArray(int size) {
            return new Intermediate[size]; }
    };  
}

这是被触发的下一个 Activity 的代码。 (我只发布了基本部分)

PrintWriter writer;
    String s;   // for testing purpose
    Swipe swipe;   // a SurfaceView

    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        swipe = new Swipe(this);
        swipe.setOnTouchListener(this);
        setContentView(swipe);
        Intent i = getIntent();
        Intermediate ob = (Intermediate)i.getParcelableExtra("printer");
        writer = (PrintWriter)ob.get();
        //s = (String)ob.get();
        //showDialog(s);   // this shows up the string I had sent, which means the string is getting passed.
    }

现在,当我不连接(即,writer 为空)并且我启动下一个活动时,什么也没有发生(没有错误)。 但是当我连接(writer 不为空)并启动下一个活动时,主活动就会崩溃。我没有费心去记录错误消息,因为我知道我在这里做错了一些事情。

任何帮助将不胜感激!谢谢!

编辑:这是异常堆栈跟踪

10-26 00:37:55.445: WARN/System.err(251): java.lang.RuntimeException: Parcel: unable to marshal value java.io.PrintWriter@44eb0b40
10-26 00:37:55.474: WARN/System.err(251):     at android.os.Parcel.writeValue(Parcel.java:1087)
10-26 00:37:55.484: WARN/System.err(251):     at android.test.Intermediate.writeToParcel(Intermediate.java:33)
10-26 00:37:55.484: WARN/System.err(251):     at android.os.Parcel.writeParcelable(Parcel.java:1106)
10-26 00:37:55.494: WARN/System.err(251):     at android.os.Parcel.writeValue(Parcel.java:1029)
10-26 00:37:55.504: WARN/System.err(251):     at android.os.Parcel.writeMapInternal(Parcel.java:469)
10-26 00:37:55.514: WARN/System.err(251):     at android.os.Bundle.writeToParcel(Bundle.java:1445)
10-26 00:37:55.524: WARN/System.err(251):     at android.os.Parcel.writeBundle(Parcel.java:483)
10-26 00:37:55.534: WARN/System.err(251):     at android.content.Intent.writeToParcel(Intent.java:5237)
10-26 00:37:55.544: WARN/System.err(251):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1204)
10-26 00:37:55.554: WARN/System.err(251):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
10-26 00:37:55.564: WARN/System.err(251):     at android.app.Activity.startActivityForResult(Activity.java:2749)
10-26 00:37:55.574: WARN/System.err(251):     at android.app.Activity.startActivity(Activity.java:2855)
10-26 00:37:55.584: WARN/System.err(251):     at android.test.TestActivity.onClick(TestActivity.java:80)
10-26 00:37:55.594: WARN/System.err(251):     at android.view.View.performClick(View.java:2364)
10-26 00:37:55.606: WARN/System.err(251):     at android.view.View.onTouchEvent(View.java:4179)
10-26 00:37:55.615: WARN/System.err(251):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
10-26 00:37:55.624: WARN/System.err(251):     at android.view.View.dispatchTouchEvent(View.java:3709)
10-26 00:37:55.634: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:37:55.634: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:37:55.644: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:37:55.656: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:37:55.664: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
10-26 00:37:55.674: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
10-26 00:37:55.684: WARN/System.err(251):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
10-26 00:37:55.694: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
10-26 00:37:55.705: WARN/System.err(251):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
10-26 00:37:55.714: WARN/System.err(251):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 00:37:55.724: WARN/System.err(251):     at android.os.Looper.loop(Looper.java:123)
10-26 00:37:55.734: WARN/System.err(251):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-26 00:37:55.744: WARN/System.err(251):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 00:37:55.754: WARN/System.err(251):     at java.lang.reflect.Method.invoke(Method.java:521)
10-26 00:37:55.764: WARN/System.err(251):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-26 00:37:55.774: WARN/System.err(251):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-26 00:37:55.785: WARN/System.err(251):     at dalvik.system.NativeStart.main(Native Method)
10-26 00:38:02.424: WARN/System.err(251): java.lang.RuntimeException: Parcel: unable to marshal value java.io.PrintWriter@44eb0b40
10-26 00:38:02.454: WARN/System.err(251):     at android.os.Parcel.writeValue(Parcel.java:1087)
10-26 00:38:02.454: WARN/System.err(251):     at android.test.Intermediate.writeToParcel(Intermediate.java:33)
10-26 00:38:02.464: WARN/System.err(251):     at android.os.Parcel.writeParcelable(Parcel.java:1106)
10-26 00:38:02.474: WARN/System.err(251):     at android.os.Parcel.writeValue(Parcel.java:1029)
10-26 00:38:02.486: WARN/System.err(251):     at android.os.Parcel.writeMapInternal(Parcel.java:469)
10-26 00:38:02.494: WARN/System.err(251):     at android.os.Bundle.writeToParcel(Bundle.java:1445)
10-26 00:38:02.505: WARN/System.err(251):     at android.os.Parcel.writeBundle(Parcel.java:483)
10-26 00:38:02.514: WARN/System.err(251):     at android.content.Intent.writeToParcel(Intent.java:5237)
10-26 00:38:02.524: WARN/System.err(251):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1204)
10-26 00:38:02.534: WARN/System.err(251):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
10-26 00:38:02.544: WARN/System.err(251):     at android.app.Activity.startActivityForResult(Activity.java:2749)
10-26 00:38:02.554: WARN/System.err(251):     at android.app.Activity.startActivity(Activity.java:2855)
10-26 00:38:02.554: WARN/System.err(251):     at android.test.TestActivity.onClick(TestActivity.java:80)
10-26 00:38:02.564: WARN/System.err(251):     at android.view.View.performClick(View.java:2364)
10-26 00:38:02.574: WARN/System.err(251):     at android.view.View.onTouchEvent(View.java:4179)
10-26 00:38:02.584: WARN/System.err(251):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
10-26 00:38:02.594: WARN/System.err(251):     at android.view.View.dispatchTouchEvent(View.java:3709)
10-26 00:38:02.604: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:38:02.614: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:38:02.625: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:38:02.634: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:38:02.644: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
10-26 00:38:02.654: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
10-26 00:38:02.664: WARN/System.err(251):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
10-26 00:38:02.674: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
10-26 00:38:02.684: WARN/System.err(251):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
10-26 00:38:02.694: WARN/System.err(251):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 00:38:02.704: WARN/System.err(251):     at android.os.Looper.loop(Looper.java:123)
10-26 00:38:02.714: WARN/System.err(251):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-26 00:38:02.714: WARN/System.err(251):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 00:38:02.724: WARN/System.err(251):     at java.lang.reflect.Method.invoke(Method.java:521)
10-26 00:38:02.734: WARN/System.err(251):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-26 00:38:02.744: WARN/System.err(251):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-26 00:38:02.754: WARN/System.err(251):     at dalvik.system.NativeStart.main(Native Method)

I've read MANY articles on how to use Parcel and I've used it in my app but it isn't working. I've just created an activity where you can connect to a computer using Sockets. I've opened a java.io.PrintWriter on the Socket's OutputStream and I want to have another activity (which is opened on touching a button in the 1st activity) use this same PrintWriter. I think the mistake I'm making here is of not cloning PrintWriter object. I don't know how to clone it.

Here's the 1st Activity (I've removed a few unneeded functions):

public class TestActivity extends Activity implements OnClickListener
{
    String ip;
    int port;
    Socket soc;
    PrintWriter writer;   // this works fine here

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        ip = "";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Connect button click handler (you can skip this part)
        Button connect = (Button)findViewById(R.id.connectBTN);
        connect.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                EditText TB = (EditText)findViewById(R.id.ipTB);
                ip = TB.getText().toString();
                TB = (EditText)findViewById(R.id.portTB);
                port = Integer.parseInt(TB.getText().toString());
                try {
                soc = new Socket(ip,port);
                showDialog("Connected successfully!");
                writer = new PrintWriter(soc.getOutputStream(), true);
                }
                catch(Exception er)
                {
                    showDialog("Couldn't connect.\n"+er.getMessage());
                    soc = null;
                }
            }
        } );

        findViewById(R.id.controlmouseBTN).setOnClickListener(this);

        // disconnect click handler  (skip this part too)
        Button disconnectBTN = (Button)findViewById(R.id.disconnectBTN);
        disconnectBTN.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                try
                {
                    if(soc.isConnected()) soc.close();
                    writer.close();
                }
                catch(Exception er) { showDialog("Couldn't close connection.\n"+er.getMessage()); }
            }
        });

    }

    @Override
    public void onClick(View v)   // here is the erroneous part 
    {
        //if(soc == null || !soc.isConnected()) { showDialog("Not connected to any computer."); return; } 
        //if(writer == null) return;
        Intent i = new Intent(getApplicationContext(), PCController.class);
        Intermediate inter = new Intermediate();   // this is the Parcelable class I've created
        //inter.set(new String("hello"));  (I tried sending a string, which worked fine, just to confirm that my Parcelable class is working)
        inter.set(writer);
        i.putExtra("printer", inter);
        startActivity(i);
    }

}

Here is the code of the Parcelable class Intermediate

import android.os.*;

public class Intermediate implements Parcelable
{
    Object ob;

    public Intermediate()
    {}

    public Intermediate(Parcel parcel)
    {
        this.ob = parcel.readValue(Intermediate.class.getClassLoader());
    }

    public void set(Object o)
    { this.ob = o; }

    public Object get()
    {
        return ob;
    }

    @Override
    public int describeContents()
    { return 0; }

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
        dest.writeValue(this.ob);
    }

   // I don't understand this part though! Just copy pasted and modified the ClassName

    public static final Intermediate.Creator<Intermediate> CREATOR = new Intermediate.Creator<Intermediate>() {
        public Intermediate createFromParcel(Parcel in) {
            return new Intermediate(in); }

        public Intermediate[] newArray(int size) {
            return new Intermediate[size]; }
    };  
}

Here's the code of the Next Activity being fired. (I've posted only the essential part)

PrintWriter writer;
    String s;   // for testing purpose
    Swipe swipe;   // a SurfaceView

    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        swipe = new Swipe(this);
        swipe.setOnTouchListener(this);
        setContentView(swipe);
        Intent i = getIntent();
        Intermediate ob = (Intermediate)i.getParcelableExtra("printer");
        writer = (PrintWriter)ob.get();
        //s = (String)ob.get();
        //showDialog(s);   // this shows up the string I had sent, which means the string is getting passed.
    }

Now when I don't connect (i.e., writer is null) and I start the next Activity, nothing happens (no errors).
But when I connect (writer is not null) and I start the next Activity, the MAIN Activity simply crashes. I didn't bother to log the error message, since I know I'm doing something really wrong here.

Any help would be appreciated! Thanks!

Edit: Here is the Exception Stack Trace

10-26 00:37:55.445: WARN/System.err(251): java.lang.RuntimeException: Parcel: unable to marshal value java.io.PrintWriter@44eb0b40
10-26 00:37:55.474: WARN/System.err(251):     at android.os.Parcel.writeValue(Parcel.java:1087)
10-26 00:37:55.484: WARN/System.err(251):     at android.test.Intermediate.writeToParcel(Intermediate.java:33)
10-26 00:37:55.484: WARN/System.err(251):     at android.os.Parcel.writeParcelable(Parcel.java:1106)
10-26 00:37:55.494: WARN/System.err(251):     at android.os.Parcel.writeValue(Parcel.java:1029)
10-26 00:37:55.504: WARN/System.err(251):     at android.os.Parcel.writeMapInternal(Parcel.java:469)
10-26 00:37:55.514: WARN/System.err(251):     at android.os.Bundle.writeToParcel(Bundle.java:1445)
10-26 00:37:55.524: WARN/System.err(251):     at android.os.Parcel.writeBundle(Parcel.java:483)
10-26 00:37:55.534: WARN/System.err(251):     at android.content.Intent.writeToParcel(Intent.java:5237)
10-26 00:37:55.544: WARN/System.err(251):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1204)
10-26 00:37:55.554: WARN/System.err(251):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
10-26 00:37:55.564: WARN/System.err(251):     at android.app.Activity.startActivityForResult(Activity.java:2749)
10-26 00:37:55.574: WARN/System.err(251):     at android.app.Activity.startActivity(Activity.java:2855)
10-26 00:37:55.584: WARN/System.err(251):     at android.test.TestActivity.onClick(TestActivity.java:80)
10-26 00:37:55.594: WARN/System.err(251):     at android.view.View.performClick(View.java:2364)
10-26 00:37:55.606: WARN/System.err(251):     at android.view.View.onTouchEvent(View.java:4179)
10-26 00:37:55.615: WARN/System.err(251):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
10-26 00:37:55.624: WARN/System.err(251):     at android.view.View.dispatchTouchEvent(View.java:3709)
10-26 00:37:55.634: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:37:55.634: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:37:55.644: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:37:55.656: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:37:55.664: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
10-26 00:37:55.674: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
10-26 00:37:55.684: WARN/System.err(251):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
10-26 00:37:55.694: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
10-26 00:37:55.705: WARN/System.err(251):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
10-26 00:37:55.714: WARN/System.err(251):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 00:37:55.724: WARN/System.err(251):     at android.os.Looper.loop(Looper.java:123)
10-26 00:37:55.734: WARN/System.err(251):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-26 00:37:55.744: WARN/System.err(251):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 00:37:55.754: WARN/System.err(251):     at java.lang.reflect.Method.invoke(Method.java:521)
10-26 00:37:55.764: WARN/System.err(251):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-26 00:37:55.774: WARN/System.err(251):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-26 00:37:55.785: WARN/System.err(251):     at dalvik.system.NativeStart.main(Native Method)
10-26 00:38:02.424: WARN/System.err(251): java.lang.RuntimeException: Parcel: unable to marshal value java.io.PrintWriter@44eb0b40
10-26 00:38:02.454: WARN/System.err(251):     at android.os.Parcel.writeValue(Parcel.java:1087)
10-26 00:38:02.454: WARN/System.err(251):     at android.test.Intermediate.writeToParcel(Intermediate.java:33)
10-26 00:38:02.464: WARN/System.err(251):     at android.os.Parcel.writeParcelable(Parcel.java:1106)
10-26 00:38:02.474: WARN/System.err(251):     at android.os.Parcel.writeValue(Parcel.java:1029)
10-26 00:38:02.486: WARN/System.err(251):     at android.os.Parcel.writeMapInternal(Parcel.java:469)
10-26 00:38:02.494: WARN/System.err(251):     at android.os.Bundle.writeToParcel(Bundle.java:1445)
10-26 00:38:02.505: WARN/System.err(251):     at android.os.Parcel.writeBundle(Parcel.java:483)
10-26 00:38:02.514: WARN/System.err(251):     at android.content.Intent.writeToParcel(Intent.java:5237)
10-26 00:38:02.524: WARN/System.err(251):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1204)
10-26 00:38:02.534: WARN/System.err(251):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
10-26 00:38:02.544: WARN/System.err(251):     at android.app.Activity.startActivityForResult(Activity.java:2749)
10-26 00:38:02.554: WARN/System.err(251):     at android.app.Activity.startActivity(Activity.java:2855)
10-26 00:38:02.554: WARN/System.err(251):     at android.test.TestActivity.onClick(TestActivity.java:80)
10-26 00:38:02.564: WARN/System.err(251):     at android.view.View.performClick(View.java:2364)
10-26 00:38:02.574: WARN/System.err(251):     at android.view.View.onTouchEvent(View.java:4179)
10-26 00:38:02.584: WARN/System.err(251):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
10-26 00:38:02.594: WARN/System.err(251):     at android.view.View.dispatchTouchEvent(View.java:3709)
10-26 00:38:02.604: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:38:02.614: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:38:02.625: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:38:02.634: WARN/System.err(251):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-26 00:38:02.644: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
10-26 00:38:02.654: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
10-26 00:38:02.664: WARN/System.err(251):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
10-26 00:38:02.674: WARN/System.err(251):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
10-26 00:38:02.684: WARN/System.err(251):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
10-26 00:38:02.694: WARN/System.err(251):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 00:38:02.704: WARN/System.err(251):     at android.os.Looper.loop(Looper.java:123)
10-26 00:38:02.714: WARN/System.err(251):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-26 00:38:02.714: WARN/System.err(251):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 00:38:02.724: WARN/System.err(251):     at java.lang.reflect.Method.invoke(Method.java:521)
10-26 00:38:02.734: WARN/System.err(251):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-26 00:38:02.744: WARN/System.err(251):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-26 00:38:02.754: WARN/System.err(251):     at dalvik.system.NativeStart.main(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野生奥特曼 2024-12-19 10:03:58

你这里有一个更大的问题。您不应该使用包裹在活动之间传递东西。根据 Android 文档:

Parcel 不是通用的序列化机制。该类(以及用于将任意对象放入 Parcel 的相应 Parcelable API)被设计为高性能 IPC 传输。因此,将任何 Parcel 数据放入持久存储中是不合适的:Parcel 中任何数据的底层实现的更改都可能导致旧数据不可读。

您应该使用捆绑包。我通常做的是在我试图在名为 toBundle 和 fromBundle 的活动之间传递的类中创建两个静态方法。 toBundle 获取我想要传输的类的实例并返回一个包。 fromBunle 获取一个包并返回该类的一个实例。

我不知道有什么方法可以在 Activity 之间传递像 PrintWriter 这样的复杂对象。但您的代码还存在其他问题。您正在主 UI 线程上执行网络操作。这会导致您的应用程序冻结很多。您需要将所有 IO 移至不同的线程。这很好,因为这样您就可以将所有 IO 内容移至可从两个活动访问的单独类中。例如,使用 IntentService。这将使您的应用程序变得更好。您可能还想阅读文章无痛线程。这是我推荐给所有 Android 开发人员的重要读物。

You've got a bigger problem here. You're not supposed use Parcels to pass things between activities. Per the android documentation:

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

You should use bundles instead. What I usually do is create two static methods in the class I'm trying to pass between activities called toBundle and fromBundle. The toBundle takes an instance of the class I want to transport and returns a bundle. The fromBunle takes a bundle and returns a instance of the class.

I'm not aware of any way to pass a complex object like a PrintWriter between Activities. But there's other problems with your code too. You're doing network stuff on the main UI thread. This is gonna cause your app to freeze A LOT. You need to move all of your IO off to a different thread. This is good because then you can just move all your IO stuff into a seperate class that's accessible from both activities. For instance, use an IntentService. This will make your app sooooooo much better. You might also want to read the article Painless Threading. It's a crucial read that I recommend for all android developers.

浮生未歇 2024-12-19 10:03:58

您无法使用任何正常的 Android 数据传递方法(Bundles / Parcel)在两个 Android 活动之间传递 PrintWriter。如果必须在两个 Activity 之间共享 PrintWriter,则可以使用静态变量。

话虽这么说,您还可以编写一个在每个活动上访问和使用的服务。您可能需要查看 ApiDemos 项目中的 LocalService 示例,了解如何执行此操作的示例。网上也有无数关于这方面的教程。

You can't pass a PrintWriter between two Android activities using any of the normal Android data-passing methods (Bundles / Parcels). If you must share the PrintWriter between two Activities, than you can use a static variable.

That being said, you could also write a service that you access and use on each Activity. You might want to look at the LocalService example in the ApiDemos project for an example on how to do that. There are also countless tutorials online regarding this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文