android - transferwd 对象调用可序列化类时应用程序崩溃

标签 android android-intent

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("controllerObject", humLogController);
        startActivity(logInActivityIntent);
}
}

我正在传递 humLogController 的实例与 intentLogInActivity ,当我调用方法时 createNewUser()传递对象后,应用程序崩溃。其他一切正常。

package com.example.praduman.humlog;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;


public class LogInActivity extends ActionBarActivity {

    private Button signUpButton;
    private Button logInButton;
    private Intent signUpChoiceIntent;
    private HumLogController humLogController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
        humLogController = (HumLogController)getIntent().getSerializableExtra("controllerObject");
        humLogController.createNewUser();
        setIntentAndButton();
    }

    private void setIntentAndButton(){
        signUpChoiceIntent = new Intent (this , SignUpChoiceActivity.class);
        signUpChoiceIntent.putExtra("controllerObject", humLogController);
        logInButton = (Button) findViewById(R.id.logInSignInButton);
        signUpButton= (Button) findViewById(R.id.logInSignUpButton);
        setActionListeners();
    }

    private void setActionListeners(){
        logInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LogInActivity.this, "hope this will work" , Toast.LENGTH_LONG).show();
            }
        });

        signUpButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(signUpChoiceIntent);
            }
        });
    }

}

这是实现 Serializable 的 Controller 类, 除了 createNewUser() ,如果我通过公开它们来调用其中一个变量,如用户名或密码。它有效,但是当我调用该方法时它崩溃了。是的,humLogModel 没有问题,我测试过。

package com.example.praduman.humlog;

import java.io.Serializable;

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

    public HumLogController() {
        this.humLogModel = new HumLogModel();
    }
    public void createNewUser(){
        humLogModel.createNewUser("TestUser4", "1234");
    }
}

The log cat is ...

07-26 15:07:23.785    8885-8885/? D/dalvikvm﹕ Late-enabling CheckJNI
07-26 15:07:23.805    8885-8891/? D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
07-26 15:07:23.905    8885-8888/? D/dalvikvm﹕ GC_CONCURRENT freed 201K, 3% free 8981K/9212K, paused 2ms+0ms, total 7ms
07-26 15:07:23.909    8885-8900/? I/dalvikvm﹕ Could not find method android.database.Cursor.getNotificationUri, referenced from method com.parse.ParseSQLiteCursor.getNotificationUri
07-26 15:07:23.909    8885-8900/? W/dalvikvm﹕ VFY: unable to resolve interface method 489: Landroid/database/Cursor;.getNotificationUri ()Landroid/net/Uri;
07-26 15:07:23.909    8885-8900/? D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
07-26 15:07:23.913    8885-8885/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
07-26 15:07:23.913    8885-8885/? W/dalvikvm﹕ VFY: unable to resolve virtual method 441: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-26 15:07:23.913    8885-8885/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-26 15:07:23.921    8885-8885/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
07-26 15:07:23.921    8885-8885/? W/dalvikvm﹕ VFY: unable to resolve virtual method 463: Landroid/content/res/TypedArray;.getType (I)I
07-26 15:07:23.921    8885-8885/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-26 15:07:23.925    8885-8885/? I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
07-26 15:07:23.925    8885-8885/? W/dalvikvm﹕ VFY: unable to resolve virtual method 404: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-26 15:07:23.929    8885-8885/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-26 15:07:23.929    8885-8885/? I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
07-26 15:07:23.929    8885-8885/? W/dalvikvm﹕ VFY: unable to resolve virtual method 406: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-26 15:07:23.929    8885-8885/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-26 15:07:23.997    8885-8888/? D/dalvikvm﹕ GC_CONCURRENT freed 268K, 4% free 9101K/9396K, paused 1ms+2ms, total 8ms
07-26 15:07:24.057    8885-8885/? D/AndroidRuntime﹕ Shutting down VM
07-26 15:07:24.057    8885-8885/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c3e648)
07-26 15:07:24.057    8885-8885/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.praduman.humlog/com.example.praduman.humlog.LogInActivity}: java.lang.NullPointerException
            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.NullPointerException
            at com.example.praduman.humlog.HumLogController.createNewUser(HumLogController.java:18)
            at com.example.praduman.humlog.LogInActivity.onCreate(LogInActivity.java:23)
            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)

最佳答案

您将 humLogModel 标记为 transient,因此它不会被序列化,并且在您的新 Activity 中制作的副本中将为 null

请注意 your constructor will not be used for the de-serialized copy :

The deserialization process does not use the object's constructor - the object is instantiated without a constructor and initialized using the serialized instance data.

因此,由于您没有序列化 humLogModel,并且您的构造函数没有机会填充 humLogModel,因此 humLogModel null 当你去使用它的时候。

关于android - transferwd 对象调用可序列化类时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31640867/

相关文章:

javascript - 经纬度和 GeolocationMarker 之间的 Google map API DirectionsService

Android - 使用 Compose 创建 Spinner

Android AppWidget如何播放Gif?

安卓 : startActivityForResult and OnActivityResult in getView() method

java - 如何创建广播接收器?

java - 将图像传递给同一类中的另一个函数

android - 蓝牙 LE 扫描过滤器不工作

android - 在 android 中谈论来电显示

Android - 谷歌地图显示位置名称而不是经度和纬度

Android:是否可以阻止特定 Activity 处理 Intent ?