0

希望没有很多代码,所以我为此道歉。我会尽量保持轻便,这意味着我不会提供所有东西(尽管如果需要我会提供)。但是我正在调试它,并将问题缩小到我提供的课程中的意图问题,但我可能是错的。我的错误如下:

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)

所以我的错误出在 onActivityResult() 方法中,我假设问题在于它无法访问我放入其中的内容。我在这里完全是在猜测,但这与意图是如何在类范围之外创建的有关,并且当调用该方法时,它找不到它,就像我猜的意图不适合那个上下文一样. 我一直在经历它,我现在什么都没有。已经在这几个小时,所以任何提示将不胜感激!

编辑:如果不是很明显,我很抱歉,但是这个类是通过我的主要活动的意图调用的,然后我在 setOnItmClickListener 方法中调用了另一个意图。只是澄清。如果需要,请让我发布任何其他内容。我只是不想用代码过度膨胀。

最新编辑:好的,所以我的头让我看了这么久,但我已经准确地指出了问题所在。它在我的 DatabaseHelper 类中将最后一个“事件”插入数据库后立即挂起。我相信我已经评论了如果你能找到它在哪里,但问题是我不知道为什么它会这样失败。我查看了我的数据库,一切都正确插入了!!但我相信一旦完成,它会返回到 onActivityResult,它就会失败。那就是我无法回答问题的地方。所以看起来一切都正确完成,但它是最后一件事,即导致问题的意图结局?我在处理意图 onActivityResult 方法的结尾时是否遗漏了什么?

数据库助手.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

时间管理器.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
...
4

2 回答 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 :/

于 2012-06-09T05:25:39.100 回答
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.

于 2012-06-09T05:32:10.180 回答