0

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:

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)

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!

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.

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?

DatabaseHelper.java

/*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+"') >= start_date and date('"+date+"') <= 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 >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}



/*SQLITEOPENHLPER INNER CLASS HERE*/


}//end of SmartCalDatabaseHelper Class

TimeManager.java

...
public void createStepsArray() {

    Cursor stepsInfo = databaseHelper.getSteps(managerId);
    for(int i=0; i < 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 < 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

MainActivity.java

...
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
...
10
  • It's not exactly clear (to me) where the error is coming from, but looks like you should research FLAG_ACTIVITY_FORWARD_RESULT.
    – K-ballo
    Jun 9, 2012 at 5:10
  • From what I am gathering from reading the Docs, it essentially sends the information already in the current intent in class B, which was launched by the previous activity(class A), to a new intent which class B is starting?
    – Andy
    Jun 9, 2012 at 5:16
  • Not really, consider an Activity A starts Activity B, then Activity B starts Activity C and finishes. When C returns, its result will be delivered to A instead of B.
    – K-ballo
    Jun 9, 2012 at 5:18
  • Ahh, ok. Hmm, I will do some more debugging, see if maybe that really is my problem, because it could definitely be it. If you want, definitely add it as an answer.Thanks!!
    – Andy
    Jun 9, 2012 at 5:20
  • I'm just suggesting it because it seems all your onActivityResult does is forward the result. Your actual problem comes from somewhere else, probably your custom adapter.
    – K-ballo
    Jun 9, 2012 at 5:21

2 Answers 2

1

From your code I understand you are calling AddManagedEventActivity and sending extra String.valueOf(id) with tag manager_id . then in your onActivityResult you want to get data from AddManagedEventActivity ?? i think you should be using intent.getExtra(..) to get data from AddManagedEventActivity this answer is based on your code because you didnt give a short explanation of your purpose :P , sorry i couldnt comment, i dont have the rep :/

4
  • Also nttice the error is an ArrayIndexOutOfBoundsException so do check your 'AddManagedEventActivity` for strings that you are returning (via putExtras in that class) . Also the code from 'AddManagedEventActivity` class where you are receiving your Intent info from this class ( String.valueOf(id) ) and the code from where you are sending back info to this class will be helpful
    – Shark
    Jun 9, 2012 at 5:29
  • Well I see where you are getting confused. I am getting the data from the last intent inside the putExtra to be sent back to my main activity. Take a closer look. It'll make sense, even though @K-ballo mentioned a way I can clean that up.
    – Andy
    Jun 9, 2012 at 5:42
  • Ok , I'm new to android btw so i've never chained activities and have them use the same intent package, but i think i understand the concept ... however all that aside your error is still an ArrayOutOfBounds , ... some activity may simply be failing to put data into the intent (giving a 'failed to deliver..') and the reason for the failure might be some undefined array indices being referred.. I was once stuck all day because i thought Java might be smart enough to automatically assign array indices to a String[] xzy = new String[](); but alas, no :/
    – Shark
    Jun 9, 2012 at 5:52
  • I made an edit to my post. And its cool, cuz I am new to android too. I'm learning by making a full blown project lol. But I have pinpointed exactly where my issue lies. Its a weird error, that honestly does not make any sense. But let me know And +1 for taking the time and and helping as well as being nice about it.
    – Andy
    Jun 9, 2012 at 7:49
1

Try limiting your loops to the size of the array:

Cursor stepsInfo = databaseHelper.getSteps(managerId);
for(int i=0; i < stepHolder.length; i++ ) { //Use stepHolder.length instead.
    stepsInfo.moveToPosition(i);
    stepHolder[i] = stepsInfo.getString(1);
}
stepsInfo.close();

Same with the for loop

for(int i=0; i < stepHolder.length; i++) { //stepHolder.length here too

Notice that the error message is showing that you have an indexOutOfBounds, i.e., you're going too deep into the array. Restrict your loops to the array size or fix the way you get numOfSteps.

8
  • It is receiving it, or at least thats the point of OnActivityResult(). It calls in startActivityForResult(), and returns it in onActivityResult()
    – Andy
    Jun 9, 2012 at 5:35
  • Could you post onActivityResult for your MainDisplayActivity Jun 9, 2012 at 5:49
  • Most certainly. Give me a sec. Though I will have to only show parts, because it has a lot of code, way to much to add to the already good amount currently. But I will add the specific parts
    – Andy
    Jun 9, 2012 at 5:51
  • K make sure you particulary add any array accessing Jun 9, 2012 at 6:04
  • rather than add the Main Activity class, I added where the arrays were. Let me know if you see anything. Cuz there are no arrays anywhere else besides in the last class I just entered.
    – Andy
    Jun 9, 2012 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.