希望没有很多代码,所以我为此道歉。我会尽量保持它尽可能轻,这意味着我不提供一切(尽管如果需要,我会)。但我正在调试它,并将问题缩小到我提供的课程中的意图问题,但我可能是错的。我的错误如下:
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方法结束时遗漏了一些东西?
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
...
答案 0 :(得分:1)
从您的代码中我了解到您正在调用AddManagedEventActivity
并发送带有标记String.valueOf(id)
的额外manager_id
。然后在你的onActivityResult
中想要从AddManagedEventActivity
获取数据?我认为您应该使用intent.getExtra(..)
从AddManagedEventActivity
获取数据这个答案是基于您的代码的,因为您没有给出您的目的的简短说明:P,抱歉我无法评论,我没有代表:/
答案 1 :(得分:1)
尝试将循环限制为数组的大小:
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();
与for循环相同
for(int i=0; i < stepHolder.length; i++) { //stepHolder.length here too
请注意,错误消息显示您有一个indexOutOfBounds,即您进入数组太深。将循环限制为数组大小或修复获得numOfSteps的方式。