Pass Activity object id to another

Asked

Viewed 588 times

1

I’m trying to pass the id from my object to another activity using Serializable, but it’s returning to me a damn mistake that I can’t fix.

Obs: my object already implements serialiable

My code is this::

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 mistake you’re making is:

10-07 19:23:28.031 18317-18317/? E/androidruntime FATAL EXCEPTION: main Process: br.com.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 cannot be cast to br.com.refsoft.refsoft.domain.Usuario 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 cannot 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)

2 answers

3


Error happens because you are not passing an object Usuario but only your id, which is an integer. Pass a class object Usuario that will work.

Note that you will need to do a "cast" (force type change) of Usuario for Serializable for the operation to work. For example, assuming the object modeloUsuario be the type Usuario:

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 pick up the id using getIntExtra() instead of getSerializableExtra().

  • Got it! Thanks mt!!!

2

You’re passing the id, a whole kind

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

And the error happens in this line below, because you are trying to recover an object of type user when in fact it is an integer number.

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

Exchange for

int id = getIntent().getIntExtra("usuario",-1);
  • Got it! Thanks mt!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.