Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly set up a startActivityForResult() with an Intent call inside setOnItemClickListener and its overridden method OnItemClick()?
    primarykey
    data
    text
    <p>Hopefully there is not a lot of code, so I apologize for that. I will try to keep it as light as possible, meaning I am not providing everything (though if needed I will). But I was debugging it, and narrowed down the issue to an intent problem in the class I am providing, but I could be wrong. My error is as follows:</p> <pre><code>6-09 00:47:15.992: E/AndroidRuntime(728): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { cmp=com.zeroe/.ListManagersActivity (has extras) }} to activity {com.zeroe/com.zeroe.MainDisplayActivity}: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 06-09 03:27:42.732: E/AndroidRuntime(5076): at com.zeroe.SmartCalDatabaseHelper.saveManagedEvent(SmartCalDatabaseHelper.java:106) 06-09 03:27:42.732: E/AndroidRuntime(5076): at com.zeroe.TimeManager.insertManagedEvents(TimeManager.java:100) </code></pre> <p>So my error is in the onActivityResult() method, and I am assuming that the issue lies in the fact that it cannot access what I put into it. I am completely guessing here, but it has something to do with how the intent was created out of the scope of the class, and when the method is invoked, it can't find it, like the intent is not for that context I guess. I have been going through it and I have nothing right now. Been at it for hours, so any hints would be greatly appreciated!</p> <p>EDIT: I apologize if it wasn't obvious, but this class was called through an intent from my main activity, and then I call another intent inside the setOnItmClickListener method. Just clarifying. And please do ask me to post anything additional if needed. I just don't want to over bloat with code.</p> <p>LATEST EDIT: Ok, so my head is killing me for looking at this for so long, but I have pin pointed exactly where the issue lies. It hangs right after it inserts the last "event" into the database in my DatabaseHelper class. I believe I have commented where that is if you can find it, but the problem, with that is I have no idea why its failing like that. I looked in my database and everything was inserted correctly!! But I believe when once its done, and it goes back to the onActivityResult, it just fails. Thats where I just can't answer the issue. So it looks like everything is done correctly, but its the very last thing, which is the intent ending that is causing the problem? Am I missing something in my way of handling the end of the intents onActivityResult method?</p> <p>DatabaseHelper.java</p> <pre><code>/*Omitting imports, but they are there*/ public class DatabaseHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "smartcal.db"; private OpenHelper openHelper; private SQLiteDatabase database; public DatabaseHelper(Context context) { openHelper = new OpenHelper(context); database = openHelper.getWritableDatabase(); } ... public Cursor getEventId(String eventName, String timeStamp) { return database.rawQuery("SELECT * FROM events WHERE time_stamp='"+timeStamp+"' AND event='"+eventName+"'", null); } public Cursor getAgendaToEdit(long id) { return database.rawQuery("SELECT * FROM events_info WHERE _id='"+id+"'", null); }//end of getAgendaToEdit method public Cursor getAgendaInfo(String date) { String query = "SELECT _id, event_name, start_date, start_time, end_date, end_time, " + "location FROM events_info WHERE date('"+date+"') &gt;= start_date and date('"+date+"') &lt;= end_date"; //add WHERE clause to filter for current day return database.rawQuery( query, null); }//end getAgendaInfo public Cursor getNumOfSteps(String id) { return database.rawQuery("SELECT _id, steps FROM managers WHERE _id='"+id+"'", null); } public Cursor getSteps(String id) { return database.rawQuery("SELECT _id, name FROM manager_steps WHERE boss='"+id+"' ORDER BY step_num", null); } public void saveManagedEvent(String eventId, String singleEventName, String location, String startDate, String startTime, String endDate, String endTime) { String[] tempDate = startDate.split("-"); startDate = ""; startDate = tempDate[2] + "-" + tempDate[0] + "-" + tempDate[1]; tempDate = endDate.split("-"); endDate = ""; endDate = tempDate[2] + "-" + tempDate[0] + "-" + tempDate[1]; ContentValues toSecondDatabase = new ContentValues(); toSecondDatabase.put("event_id", eventId); toSecondDatabase.put("event_name", singleEventName); toSecondDatabase.put("start_date", startDate); toSecondDatabase.put("start_time", startTime); toSecondDatabase.put("end_date", endDate); toSecondDatabase.put("end_time", endTime); toSecondDatabase.put("location", location); database.insert("events_info", null, toSecondDatabase); /*HANGS HERE FOR SOME REASON AND GIVES ME THE ABOVE ERROR*/ } public String saveSingleEvent(String event) { String timeStamp = getTimeStamp(); ContentValues toFirstDatabase = new ContentValues(); toFirstDatabase.put("event", event); toFirstDatabase.put("time_stamp", timeStamp); database.insert("events", null, toFirstDatabase); Cursor eventCursor = getEventId(event, timeStamp); eventCursor.moveToFirst(); int eventId = eventCursor.getInt(0); eventCursor.close(); return String.valueOf(eventId); } public String getTimeStamp() { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a"); return sdf.format(new Date()); } private static String pad(int c) { if (c &gt;= 10) return String.valueOf(c); else return "0" + String.valueOf(c); } /*SQLITEOPENHLPER INNER CLASS HERE*/ }//end of SmartCalDatabaseHelper Class </code></pre> <p>TimeManager.java</p> <pre><code>... public void createStepsArray() { Cursor stepsInfo = databaseHelper.getSteps(managerId); for(int i=0; i &lt; numOfSteps; i++ ) { stepsInfo.moveToPosition(i); stepHolder[i] = stepsInfo.getString(1); } stepsInfo.close(); }//end of createStepsArray method public void insertManagedEvents() { Calendar now = Calendar.getInstance(); String id = databaseHelper.saveSingleEvent(eventName); for(int i=0; i &lt; numOfSteps; i++) { if(i == 0) now.add(Calendar.DAY_OF_MONTH, addBetweenEvents - 1); else now.add(Calendar.DAY_OF_MONTH, addBetweenEvents); if(i == numOfSteps-1) { databaseHelper.saveManagedEvent(id, stepHolder[i], location, new StringBuilder().append(eYear).append("-").append(pad(eMonth)).append(pad(eDay)).toString(), "00:00", new StringBuilder().append(eYear).append("-").append(pad(eMonth)).append(pad(eDay)).toString(), "00:00"); } else { databaseHelper.saveManagedEvent(id, stepHolder[i], location, new StringBuilder().append(now.get(Calendar.YEAR)).append("-").append(pad(now.get(Calendar.MONTH)+1)).append("-").append(pad(now.get(Calendar.DAY_OF_MONTH))).toString(), "00:00", new StringBuilder().append(now.get(Calendar.YEAR)).append("-").append(pad(now.get(Calendar.MONTH)+1)).append("-").append(pad(now.get(Calendar.DAY_OF_MONTH))).toString(), "00:00"); } } }//end of insertManagedEvents method </code></pre> <p>MainActivity.java</p> <pre><code>... public void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == EVENT_ENTRY_REQUEST_CODE) { if(resultCode == RESULT_OK) { //send the database the info sent back from add event activity databaseHelper.saveSimpleEvent(data.getStringExtra("event"),data.getStringExtra("location"), data.getStringExtra("start_date"), data.getStringExtra("start_time"), data.getStringExtra("end_date"), data.getStringExtra("end_time")); agendaAdapter.changeCursor(databaseHelper.getAgendaInfo(getChosenDate())); } } if(requestCode == MANAGED_EVENT_LIST_REQUEST_CODE) { if(resultCode == RESULT_OK) { //Do stuff when managed list is working alright, and //after user enters necessary info, in order to create the events into the database TimeManager timeManager = new TimeManager(databaseHelper, data.getStringExtra("manager_id"), data.getStringExtra("event_name"), data.getStringExtra("events_subject"), data.getStringExtra("location"), data.getStringExtra("end_time")); if(timeManager.checkValidManager()== false) { //tell user it failed due to the days not being correct in order to manage time } else { timeManager.createStepsArray(); timeManager.insertManagedEvents(); /*OR HANGS HERE MORE LIKELY SINCE THIS IS WHERE THE ERROR SAYS*/ } } } }//end of onActivityForResult Method ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload