当我尝试使用intent传递对象时,它会崩溃我的应用程序

时间:2015-07-25 15:25:15

标签: java android serializable

当我不使用putExtra时,应用程序运行良好,但是当我使用putExtra并传递对象时它会崩溃。我也尝试在HumLogController类中实现Serializable(我想传递的对象)。结果与parceable相同,即崩溃。

package com.example.praduman.humlog;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import com.parse.Parse;
import java.io.Serializable;

public class HomeActivity extends ActionBarActivity {

    private HumLogController humLogController;
    private Intent logInActivityIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        humLogController = new HumLogController();      


        logInActivityIntent = new Intent(this , LogInActivity.class);
        logInActivityIntent.putExtra("my object", (Serializable) humLogController);
        startActivity(logInActivityIntent);
    }


}

活动日志是: - 当我不使用putExtra时,它不会崩溃。任何人都可以帮忙..?下面是android studio中的log cat。 正如我所说,我尝试实现Serializable和Parceable,但它们不起作用。

07-25 11:21:50.294  14241-14241/com.example.praduman.humlog D/dalvikvm﹕ Late-enabling CheckJNI
07-25 11:21:50.462  14241-14241/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
07-25 11:21:50.466  14241-14241/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 440: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-25 11:21:50.466  14241-14241/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-25 11:21:50.466  14241-14241/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
07-25 11:21:50.466  14241-14241/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 462: Landroid/content/res/TypedArray;.getType (I)I
07-25 11:21:50.466  14241-14241/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-25 11:21:50.474  14241-14241/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
07-25 11:21:50.474  14241-14241/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 403: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-25 11:21:50.474  14241-14241/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-25 11:21:50.474  14241-14241/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
07-25 11:21:50.474  14241-14241/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 405: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-25 11:21:50.478  14241-14241/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-25 11:21:50.526  14241-14241/com.example.praduman.humlog D/AndroidRuntime﹕ Shutting down VM
07-25 11:21:50.526  14241-14241/com.example.praduman.humlog W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c47648)
07-25 11:21:50.530  14241-14241/com.example.praduman.humlog E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.praduman.humlog/com.example.praduman.humlog.HomeActivity}: java.lang.ClassCastException: com.example.praduman.humlog.HumLogController cannot be cast to java.io.Serializable
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: com.example.praduman.humlog.HumLogController cannot be cast to java.io.Serializable
            at com.example.praduman.humlog.HomeActivity.onCreate(HomeActivity.java:32)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
07-25 11:21:50.534  14241-14241/com.example.praduman.humlog D/dalvikvm﹕ GC_FOR_ALLOC freed 291K, 4% free 9017K/9336K, paused 6ms, total 6ms
07-25 11:21:50.558  14241-14256/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.database.Cursor.getNotificationUri, referenced from method com.parse.ParseSQLiteCursor.getNotificationUri
07-25 11:21:50.558  14241-14256/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve interface method 488: Landroid/database/Cursor;.getNotificationUri ()Landroid/net/Uri;
07-25 11:21:50.558  14241-14256/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002

当我没有在HumLogController中实现Serilizable并且只是在对象中转换对象时,会发生上述错误。下面是我在那里实现Serializable时HumLogController类的代码。

package com.example.praduman.humlog;

import java.io.Serializable;

/**
 * Created by Praduman on 23/07/2015.
 */
public class HumLogController implements Serializable{
   private HumLogModel humLogModel ;
    private String username;
    private String password;
    private String userType;

    public HumLogController() {
        this.humLogModel = new HumLogModel();
    }
    public void createNewUser(){
        humLogModel.createNewUser(getUsername(), getPassword());
    }

    public void setUserEssentials(String username,String password, String userType){
        setUsername(username); setPassword(password); setUserType(userType);
    }

    public void setUsername(String username){
        this.username = username;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public void setUserType(String userType){
        this.userType = userType;
    }

    public String getUsername(){
        return username;
    }
    public String getPassword(){
        return password;
    }
    public String getUserType(){
        return userType;
    }
}

我在HumLogContoller类中实现serializable时的logcat如下所示。

07-25 11:42:23.838  23311-23311/? D/dalvikvm﹕ Late-enabling CheckJNI
07-25 11:42:24.058  23311-23330/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.database.Cursor.getNotificationUri, referenced from method com.parse.ParseSQLiteCursor.getNotificationUri
07-25 11:42:24.058  23311-23330/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve interface method 488: Landroid/database/Cursor;.getNotificationUri ()Landroid/net/Uri;
07-25 11:42:24.058  23311-23330/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
07-25 11:42:24.102  23311-23331/com.example.praduman.humlog D/dalvikvm﹕ GC_FOR_ALLOC freed 283K, 4% free 9025K/9336K, paused 4ms, total 4ms
07-25 11:42:24.118  23311-23311/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
07-25 11:42:24.118  23311-23311/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 440: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-25 11:42:24.118  23311-23311/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-25 11:42:24.122  23311-23311/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
07-25 11:42:24.126  23311-23311/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 462: Landroid/content/res/TypedArray;.getType (I)I
07-25 11:42:24.126  23311-23311/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-25 11:42:24.130  23311-23311/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
07-25 11:42:24.130  23311-23311/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 403: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-25 11:42:24.130  23311-23311/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-25 11:42:24.150  23311-23311/com.example.praduman.humlog I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
07-25 11:42:24.150  23311-23311/com.example.praduman.humlog W/dalvikvm﹕ VFY: unable to resolve virtual method 405: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-25 11:42:24.150  23311-23311/com.example.praduman.humlog D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-25 11:42:24.206  23311-23311/com.example.praduman.humlog D/AndroidRuntime﹕ Shutting down VM
07-25 11:42:24.206  23311-23311/com.example.praduman.humlog W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c47648)
07-25 11:42:24.222  23311-23311/com.example.praduman.humlog D/dalvikvm﹕ GC_FOR_ALLOC freed 381K, 5% free 9147K/9560K, paused 13ms, total 14ms
07-25 11:42:24.222  23311-23311/com.example.praduman.humlog E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.praduman.humlog/com.example.praduman.humlog.HomeActivity}: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.praduman.humlog.HumLogController)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.praduman.humlog.HumLogController)
            at android.os.Parcel.writeSerializable(Parcel.java:1285)
            at android.os.Parcel.writeValue(Parcel.java:1233)
            at android.os.Parcel.writeMapInternal(Parcel.java:591)
            at android.os.Bundle.writeToParcel(Bundle.java:1646)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.content.Intent.writeToParcel(Intent.java:6831)
            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1927)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
            at android.app.Activity.startActivityForResult(Activity.java:3390)
            at android.app.Activity.startActivityForResult(Activity.java:3351)
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
            at android.app.Activity.startActivity(Activity.java:3587)
            at android.app.Activity.startActivity(Activity.java:3555)
            at com.example.praduman.humlog.HomeActivity.onCreate(HomeActivity.java:33)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.io.NotSerializableException: com.example.praduman.humlog.HumLogModel
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
            at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
            at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
            at android.os.Parcel.writeSerializable(Parcel.java:1280)
            at android.os.Parcel.writeValue(Parcel.java:1233)
            at android.os.Parcel.writeMapInternal(Parcel.java:591)
            at android.os.Bundle.writeToParcel(Bundle.java:1646)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.content.Intent.writeToParcel(Intent.java:6831)
            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1927)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
            at android.app.Activity.startActivityForResult(Activity.java:3390)
            at android.app.Activity.startActivityForResult(Activity.java:3351)
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
            at android.app.Activity.startActivity(Activity.java:3587)
            at android.app.Activity.startActivity(Activity.java:3555)
            at com.example.praduman.humlog.HomeActivity.onCreate(HomeActivity.java:33)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

如果你有一个像HumLogController这样的自定义Serializable类,你必须确保它的所有字段都是Serializable,或者如果你不想序列化它们,则声明它们transient。在您的情况下,HumLogModel不是可序列化的。