1

I need to start an activity with details of some data passing a parameter that I get from my Firebase DB on my Intent, but everytime I click an item on the Adapter my app crashes.

I have an activity like this:

And need to open something like this:

Here is my Adapter.setOnClickListener code:

viewHolder.eventCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent eventIntent = new Intent(getContext(), EventActivity.class);
                    eventIntent.putExtra("event_id", event.getName());
                    startActivity(eventIntent);
                }
            });

And here is my new activity code to get that parameter:

package br.com.ministeriosonhodedeus.sonhodedeus.activity;

import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import br.com.ministeriosonhodedeus.sonhodedeus.R;
import br.com.ministeriosonhodedeus.sonhodedeus.domain.Event;
import br.com.ministeriosonhodedeus.sonhodedeus.fragments.EventFragment;

public class EventActivity extends BaseActivity {

private Event e;


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

    setUpToolbar();
    e = getIntent().getParcelableExtra("event_id");
    getSupportActionBar().setTitle(e.getName());
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ImageView appBarImg = (ImageView) findViewById(R.id.appBarImg);
    Picasso.with(getContext()).load(e.geturl_foto()).into(appBarImg);
    if (savedInstanceState == null) {
        EventFragment frag = new EventFragment();
        frag.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.EventFragment, frag).commit();
    }
}

public void setTitle(String s) {
    CollapsingToolbarLayout c = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    c.setTitle(s);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            ActivityCompat.finishAfterTransition(getActivity());
            return true;
    }
    return super.onOptionsItemSelected(item);
}
}

Here is my error 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.EventActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String br.com.ministeriosonhodedeus.sonhodedeus.domain.Event.getName()' 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.Event.getName()' on a null object reference
                                                                                          at br.com.ministeriosonhodedeus.sonhodedeus.activity.EventActivity.onCreate(EventActivity.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) 

Any idea of what went wrong? Thanks!

  • 2
    Please add the logcat from the crash. – Chris Gomez Mar 20 '18 at 19:25
  • show full code of event activity... – Wahdat Jan Mar 21 '18 at 06:29
  • What is the error from your logcat? If you are interested, **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can retrieve data from Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Mar 21 '18 at 09:37
  • @CristianGomez there is my logcat error – Adriano Junior Mar 21 '18 at 17:37
  • @AlexMamo added my logcat error. I already can display data in my Adapter, the problem is when I need to pass something like this data id of my DB and retrieve it on another screen to show more details about it like shown in the images. – Adriano Junior Mar 21 '18 at 17:40
  • @WahdatKashmiri full code is now in the question – Adriano Junior Mar 21 '18 at 17:41

1 Answers1

1

To solve this, please change this line of code:

e = getIntent().getParcelableExtra("event_id");

with

String event_id = (String) getIntent().getExtras().get("event_id");

and this:

getSupportActionBar().setTitle(e.getName());

with

getSupportActionBar().setTitle(event_id);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Why that cast? Remove `(Event) `. You are passing a String not an Event object. It works? – Alex Mamo Mar 21 '18 at 18:28
  • Still not working, app keeps crashing anyway What if I get the selected recycler item position, is there a way of doing this and retrieving the adapter's data? – Adriano Junior Mar 21 '18 at 18:32
  • Is crashing with what message? **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can display data from a Firebase Realtime database in a RecyclerView using FirebaseRecyclerAdapter. Just, setOnClickListener on the item and pass the Intent. But you'll have the same problem. You are passing a String and you expect an object of type `Event`. Just use the String as I showed or instead of passing that String, pass an object of `Event` class and use your code. `eventIntent.putExtra("event_id", event);` Works? – Alex Mamo Mar 21 '18 at 18:42
  • this is my logcat messages from the onClick method [link](https://imgur.com/a/qn475) – Adriano Junior Mar 21 '18 at 19:01
  • Yes, it's casting problem. Have you tried to change only this line `eventIntent.putExtra("event_id", event.getName());` with this `eventIntent.putExtra("event_id", event);`? The rest of the code remains unretouched. It works? – Alex Mamo Mar 21 '18 at 19:04
  • Not really, is there a way of passing my complete object by an Intent, so I can retrieve it, like using parcelable or serializable? – Adriano Junior Mar 23 '18 at 17:38
  • Yes there is. Change this `eventIntent.putExtra("event_id", event.getName());` with `eventIntent.putExtra("event", event);`. Make your Event class implement Serializable. And in the second activity just use: `Event event = (Event) getIntent().getSerializableExtra("event");` Give it a try. Works? – Alex Mamo Mar 23 '18 at 17:44
  • No, tried it and app keeps crashing when any item is clicked on my Adapter. – Adriano Junior Mar 24 '18 at 20:34
  • Please edit your post by adding the changed code to better where the problem is. And please add also the error that you got. – Alex Mamo Mar 25 '18 at 07:27
  • Solved the problem, just did what you said and there's no null object anymore. Thanks. – Adriano Junior Apr 04 '18 at 17:23