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?