-7

EDIT: I'm not trying to pass data from the fragment to it's container activity. I'm trying to pass data from the fragment to a DIFFERENT activity. ALSO, my post isn't about the error that I got; I just put that there for reference. My one and only question to this post was, "Can someone tell me if I'm implementing the interface correctly?"

I'm having trouble passing an object from a fragment that's in the main activity to a different activity. I'm using an interface to try and pass the object between the fragment and activity.

My app is an app that shows the events on campus via Google Maps and its markers. The Google Map is implemented inside the fragment.

When a user clicks on a marker, an event object is supposed to be passed to an activity and a screen pops up showing the details of the event object that was passed.

Below is the interface in the NowMapFragment class (fragment with the Google Map) and the onMarkerClick event for when someone presses on a marker

private GoogleMap mMap;
private OnFragmentInteractionListener mListener;
ArrayList<Event> events = Event.getEvents();

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        mListener = (OnFragmentInteractionListener) activity;
    }
    catch (Exception e) {

    }
}

@Override
public boolean onMarkerClick(Marker marker) {
    Intent intent = new Intent();

    for(Event event : events) {
        if(marker.getTitle().equals(event.getName())) {
            mListener.setEvent(event);
        }
    }

    intent = new Intent(getActivity(), EventDetails.class);
    startActivity(intent);

    return true;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
    void setEvent(Event event);
}

This is the activity that I'm trying to pass the event object to

package com.group4.group4;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class EventDetails extends AppCompatActivity implements NowMapFragment.OnFragmentInteractionListener {

    Event event;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_details);
        /*FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.map, new NowMapFragment()).commit();*/
        setTitle(event.getName());

        TextView eventDescription = (TextView) findViewById(R.id.event_description);
        TextView eventLocation = (TextView) findViewById(R.id.event_location);
        TextView eventDate = (TextView) findViewById(R.id.event_date);
        TextView eventTime = (TextView) findViewById(R.id.event_time);
        final TextView eventAttendance = (TextView) findViewById(R.id.event_attendance);
        Button checkIn = (Button) findViewById(R.id.check_in);

        eventDescription.setText(event.getDescription());
        eventLocation.setText("Location: " + event.getLocation());
        eventDate.setText("Date: Tuesday, December 5, 2017");
        eventTime.setText("Time: 9:30 AM to 12:15 PM");
        eventAttendance.setText("Current Attendance: " + Integer.toString(event.getAttendance()));
        checkIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int counter = event.getAttendance();
                event.setAttendance(++counter);
                eventAttendance.setText("Current Attendance: " + Integer.toString(event.getAttendance()));
                Toast toast = Toast.makeText(getApplicationContext(), "Successful check in.", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
                toast.show();
            }
        });
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void setEvent(Event event) {
        this.event = event;
    }
}

I feel like I'm just misunderstanding how to implement the interface, and implemented it wrong.

Also, my exact error is:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.group4.group4.Event.getName()' on a null object reference

Can someone tell me if I'm implementing the interface correctly?

chataolauj
  • 99
  • 1
  • 15
  • would u mind show your Event class – Iqbal Rizky Dec 11 '17 at 05:10
  • what you actually trying to do... are you trying to the data from fragment to new activity or within the same activity where fragment is present – Devil10 Dec 11 '17 at 05:13
  • if you trying to do it within same activity then whenever you add fragment to activity the you have to call your fragment listener and not in oncreate because every time your activity opens your event will be null at first time and it will show nullpointer – Devil10 Dec 11 '17 at 05:15
  • Pls refer this answer https://stackoverflow.com/questions/46150954/call-method-from-fragment-to-activity/46151084#46151084 – Manoj Bhadane Dec 11 '17 at 05:19

3 Answers3

0

Fragment Attached to same Activity you can try this,

((YourActivity)getActivity()).getYourObjects();
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
0

You can use setArguments and getArguments as below

In Fragment use setArguments

Bundle bundle = new Bundle();
bundle.putString("event", event);
intent = new Intent(getActivity(), EventDetails.class);
intent.setArguments(bundle)
startActivity(intent);

In Activity

use getArguments

Event event = getArguments().get("event");
0

The error occurs, because you are calling event.getName() in your onCreate() - method of your activity.

You should have a look at the activity's lifecycle. Your globale property event is not initialized on onCreate()

You should pass your Event using

intent = new Intent(getActivity(), EventDetails.class);
intent.putExtra("event", event);
startActivity(intent);

and in your activity call in your onCreate()

Event event = (Event) getIntent().getSerializableExtra("event");

Note: your class Event should implement Serializable interface to do this

beal
  • 435
  • 4
  • 15