Pass activity object id to another

1

I'm trying to pass id from my object to another activity using Serializable , but it's returning me a damn error that I can not fix.

  

Obs: My object already implements serialiable

My code is as follows:

if (view == btnTeste) {
        try {
            repositorioUsuario = new RepositorioUsuario(this);
            repositorioUsuario.insertUsuario(inserirUsuarioTeste());

            Intent it = new Intent(this, Parceble.class);
            it.putExtra("usuario", modeloUsuario.getId());
            startActivity(it);
        } catch (Exception e) {
            Log.i(CATEGORIA, e.toString());
        }
    }

And in the other activity:

Usuario user = (Usuario) getIntent().getSerializableExtra("usuario");

    int id = 1;
    if(user != null){
        id = user.getId();
        if ( id != 0){
            Toast.makeText(Parceble.this, "id: " + id, Toast.LENGTH_SHORT).show();
        }
    }

And the error you are giving is:

  

10-07 19: 23: 28,031 18317-18317 /? E / AndroidRuntime: FATAL EXCEPTION: main       Process: br.refsoft.refsoft, PID: 18317       java.lang.RuntimeException: Unable to start activity ComponentInfo {br.com.refsoft.refsoft / br.com.refsoft.refsoft.activity.Parceble}: java.lang.ClassCastException: java.lang.Integer can not be cast to br. com.refsoft.refsoft.domain.User               at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2184)               at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2233)               at android.app.ActivityThread.access $ 800 (ActivityThread.java:135)               at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1196)               at android.os.Handler.dispatchMessage (Handler.java:102)               at android.os.Looper.loop (Looper.java:136)               at android.app.ActivityThread.main (ActivityThread.java:5001)               at java.lang.reflect.Method.invokeNative (Native Method)               at java.lang.reflect.Method.invoke (Method.java:515)               at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:785)               at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:601)               at dalvik.system.NativeStart.main (Native Method)        Caused by: java.lang.ClassCastException: java.lang.Integer can not be cast to br.com.refsoft.refsoft.domain.Usuario               at br.com.refsoft.refsoft.activity.Parceble.onCreate (Parceble.java:18)               at android.app.Activity.performCreate (Activity.java:5231)               at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1087)               at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2148)   at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2233)   at android.app.ActivityThread.access $ 800 (ActivityThread.java:135)   at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1196)   at android.os.Handler.dispatchMessage (Handler.java:102)   at android.os.Looper.loop (Looper.java:136)   at android.app.ActivityThread.main (ActivityThread.java:5001)   at java.lang.reflect.Method.invokeNative (Native Method)

    
asked by anonymous 08.10.2015 / 01:31

2 answers

3

The error happens because you are not passing an object Usuario in the intent, but only your id, which is an integer. Pass an object of class Usuario that will work.

Note that you will need to cast% from% to Usuario for the operation to work. For example, assuming that the Serializable object is of type modeloUsuario :

it.putExtra("usuario", (Serializable)modeloUsuario);

Now if your intention is to actually pass only the id and not the entire user, then the problem lies at the other end. In the second Activity get the id using Usuario instead of getIntExtra() .

    
08.10.2015 / 01:46
2

You are passing the id, an integer type

it.putExtra("usuario", modeloUsuario.getId());

And the error happens in this line below, since you are trying to recover a User object when it is actually an integer.

Usuario user = (Usuario) getIntent().getSerializableExtra("usuario");

Switch to

int id = getIntent().getIntExtra("usuario",-1);
    
08.10.2015 / 07:31