Pass data from Firebase Database by Intent and retrieve in another activity [duplicate]

0

This is the fragment of my Fragment code in which I need to pass a parameter (Firebase DB id) 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 submitted 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:

When selecting the item I 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 anyone tell me what's wrong?

    
asked by anonymous 22.03.2018 / 00:30

1 answer

1

I'll leave an example of how to move an object from one screen to another just by implementing 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

    
22.03.2018 / 02:45