我有一个包含电影的列表视图。该列表在 strings.xml 中声明。用户可以将电影添加到此列表视图,可以编辑条目并可以删除条目。
我正在实现一个新的“插入”方法,它允许用户突出显示(即,使用火箭头,使用鼠标的轨迹球)每个条目,并在所选条目的正下方插入一个新电影。我的问题是,当我从插入活动中设置结果时,我的代码中出现了 arrayindexoutofbounds 错误:
我的 OnActivityResult 案例之一:
case INSERT_MOVIE:
Lab8_082588FetchDetails insertMovie = new Lab8_082588FetchDetails();
ListView list = getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
NumberFormat formatterInsert = new DecimalFormat("###,###,###");
insertMovie.setTitle(data
.getStringExtra(Lab8_082588Insert.TITLE_STRING));
insertMovie.setGross("$"
+ formatterInsert.format(Double.parseDouble(data
.getStringExtra(Lab8_082588Insert.GROSS_STRING))));
insertMovie.setDate(data
.getStringExtra(Lab8_082588Insert.DATE_STRING));
results.add(insertMovie);
adapter.notifyDataSetChanged();
我的 OnOptionsItemSelected:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection using item.getItemId()
switch (item.getItemId()) {
case R.id.add:
add();
break;
case R.id.insert:
insert(item);
break;
}
return true;
}
private void insert(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
Intent insertData = new Intent(Lab8_082588.this,
Lab8_082588Insert.class);
insertData.putExtra(Lab8_082588.CURRENT_ROW, info.position); //nullPointerException
startActivityForResult(insertData, Lab8_082588.INSERT_MOVIE);
}
我的插入类:
public class Lab8_082588Insert extends Activity {
public static final String TITLE_STRING = "TITLE_STRING";
public static final String GROSS_STRING = "GROSS_STRING";
public static final String DATE_STRING = "DATE_STRING";
public static final String PASSED_ROW = "PASSED_ROW";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addeditinsert);
Intent rowNumber = getIntent();
results.add(data.getIntExtra(Lab8_082588Insert.PASSED_ROW, 0) + 1, insertMovie);
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu3, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection using item.getItemId()
switch (item.getItemId()) {
case R.id.insert2:
next();
break;
}
return true;
}
private void next() {
// TODO Auto-generated method stub
EditText movieTitle = (EditText) findViewById(R.id.etTitle);
EditText movieGross = (EditText) findViewById(R.id.etGross);
EditText movieDate = (EditText) findViewById(R.id.etDate);
String title = movieTitle.getText().toString();
String gross = movieGross.getText().toString();
String date = movieDate.getText().toString();
if ((title.length() > 0) && (gross.length() > 0)
&& (date.length() == 4)) {
Intent data = getIntent();
int rowNumber = getIntent().getIntExtra(Lab8_082588.CURRENT_ROW, 0);
data.putExtra(Lab8_082588Insert.TITLE_STRING, title);
data.putExtra(Lab8_082588Insert.GROSS_STRING, gross);
data.putExtra(Lab8_082588Insert.DATE_STRING, date);
data.putExtra(Lab8_082588Insert.PASSED_ROW, rowNumber);
setResult(RESULT_OK, data);
finish();
}
}