I have a DataHandaling
class that is used to do the logic, it is also suppose to be an easier way to retrieve an ArrayList
from multiple activities, But when I start a new activity the ArrayList
is null
(NOTE: The array is not empty because I have used the same array multiple times in the main activity, this problem is only a case when I start a new activity)
The code from DataHandaling
public class DataHandaling {
EnviroClass enviroClass = new EnviroClass();
private ArrayList<Event> eventArray = new ArrayList<Event>();
public ArrayList<Event> getEventArray() {
return eventArray;
}
public void getEvents() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(enviroClass.url() + "/api/events/" + 7)
.build();
client.newCall(request)
.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject eventObject = new JSONObject(response.body().string());
JSONArray eventJsonArray = eventObject.getJSONArray("events");
for (int i = 0; i<eventJsonArray.length(); i++) {
eventObject = eventJsonArray.getJSONObject(i);
eventObject = eventObject.getJSONObject("event");
eventArray.add(new Event(eventObject.getString("name"), eventObject.getString("address"), eventObject.getString("image"), "100" ,eventObject.getString("start_date"), eventObject.getString("end_date"), eventObject.getInt("id")));
}
EventListAdapter adapter = new EventListAdapter(null, null);
adapter.swap(eventArray);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
This is where it is breaking in the new activity
textView.setText(dh.getEventArray().get(0).getName());
The interaction listener to open the new activity
@Override
public void onListFragmentInteraction(int id) {
Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
intent.putExtra("id", id);
MainActivity.this.startActivity(intent);
}
The activity where it is breaking
public class ProfileActivity extends AppCompatActivity {
DataHandaling dh = new DataHandaling();
int eventArrayId;
TextView eventDescription;
TextView eventName;
ImageView eventImage;
ArrayList<Event> eventArray = new ArrayList<Event>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
getSupportActionBar().setHomeButtonEnabled(true);
Intent intent = getIntent();
eventArrayId = intent.getIntExtra("id", 0);
eventName = (TextView) findViewById(R.id.eventNameTextView);
eventName.setText(dh.getEventArray().get(0).getName());
}
}
Stack Trace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.gb.socilizer_app/com.app.gb.socilizer.Activities.ProfileActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.app.garethbecker.socializer.Activities.ProfileActivity.onCreate(ProfileActivity.java:39)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)