Pass data from Firebase Database to Intent and recover in another Activity

Asked

Viewed 464 times

0

This is the code snippet of my Fragment in which I need to pass a parameter (id of Firebase DB) through an Intent generated by Adapter.setOnClickListener:

...
viewHolder.eventoCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String evento_id = evento.getId();
                    Intent intent = new Intent(getContext(), EventoActivity.class);
                    intent.putExtra("evento_id", evento_id);
                    startActivity(intent);
                    Log.i(TAG_CLICK, "clicou no evento...");
                }
            });
...

And this is the Activity code that needs to retrieve the sent Id to show more detailed data:

public class EventoActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_evento);

    setUpToolbar();

    Intent intent = getIntent();
    if (intent.hasExtra("evento_id")) {
        Evento e = getIntent().getParcelableExtra("evento_id");
        getSupportActionBar().setTitle(e.getNome());
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ImageView appBarImg = (ImageView) findViewById(R.id.appBarImg);
        Picasso.with(getContext()).load(e.geturl_foto()).into(appBarImg);

        if (savedInstanceState == null) {
            EventoFragment frag = new EventoFragment();
            frag.setArguments(getIntent().getExtras());

            getSupportFragmentManager().beginTransaction().add(R.id.EventoFragment, frag).commit();
        }
    }
}

I have an Activity like this:

by selecting the item need to open something like this other Activity:

This is the error shown in my logcat:

03-21 14:18:12.390 27190-27190/br.com.ministeriosonhodedeus.sonhodedeus E/AndroidRuntime: FATAL EXCEPTION: main
   Process: br.com.ministeriosonhodedeus.sonhodedeus, PID: 27190
   java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.ministeriosonhodedeus.sonhodedeus/br.com.ministeriosonhodedeus.sonhodedeus.activity.EventoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String br.com.ministeriosonhodedeus.sonhodedeus.domain.Evento.getNome()' on a null object reference
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
   at android.app.ActivityThread.-wrap12(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6123)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String br.com.ministeriosonhodedeus.sonhodedeus.domain.Evento.getNome()' on a null object reference
   at br.com.ministeriosonhodedeus.sonhodedeus.activity.EventoActivity.onCreate(EventoActivity.java:30)
   at android.app.Activity.performCreate(Activity.java:6672)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
   at android.app.ActivityThread.-wrap12(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:154) 
   at android.app.ActivityThread.main(ActivityThread.java:6123) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

Can someone tell me what’s wrong?

  • 1

    From what I understand you’re passing a string id (String evento_id = evento.getId();) to the other Activity but is using it as if it were an object (Evento e = getIntent().getParcelableExtra("evento_id");), I believe the error lies in logic

  • @Guilhermecostamilam tried as follows in Activity that receives the parameters and continued with the same error String event_id = (String) getIntent().getExtras().get("event_id");

  • Yes because even if you change the type of the variable to String you are still using the variable e as an object (e.getNome()), you must do a new search on firebase to grab the object from the id or pass the whole object from one Activity to another

  • @Guilhermecostamilam how can I pass the whole object to the Intent? Because I will need to retrieve the object with its respective BD attributes in the next Activity.

  • Would it have to be something like that? Evento e = new Evento("evento_id", evento.getId());
 Intent intent = new Intent(getContext(), EventoActivity.class);
 intent.putExtra("evento_id", e.getId());

1 answer

1

I’ll leave an example of how to eviar an object from one screen to another just implement by swapping the get set and class.

Screen 1:

btnOK.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {

            /* Instanciando objeto que será enviado para a Tela 2 */
            Usuario u = new Usuario();

            /* Recebendo os valores no objeto para enviar para
               a próxima tela através do método putExtra() */
            u.setLogin("usuario tal");
            u.setSenha("123");

            Intent it = new Intent(MainActivity.this, Tela2.class);

            //Passando os valores
            it.putExtra("usuario", u);

            startActivity(it);

            //Opcional: encerrar a activity
            //finish();
       }
 });

Screen 2:

//Recebendo os valores passados como String
Usuario u = (Usuario) getIntent().getSerializableExtra("usuario");
String login = u.getLogin();
String senha = u.getSenha();

Class:

public class Usuario implements Serializable {
    //...
}

Source: Github

Browser other questions tagged

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