【发布时间】:2015-12-07 09:36:34
【问题描述】:
我真的不知道如何解决这个问题。
在我的一个活动中,它有一个ListView。单击列表时,它会意图对新的 Activity 进行编辑。现在的问题是当我在模拟器中按下后退按钮时,应用程序崩溃了。
listViewUpdate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
mClickedPosition=position; // update
// Get the cursor, positioned to the corresponding listview_item_row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this listview_item_row in the database.
ID =
cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
Intent intent = new Intent(getActivity(), Edit_Details.class);
intent.putExtra("ID", ID);
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
if (requestCode == PROJECT_REQUEST_CODE) {
ReceiveProject = data.getStringExtra("project1");
ReceiveDescription = data.getStringExtra("description");
ReceiveProgress = data.getIntExtra("progress", 0);
ReceiveTimeIn = data.getStringExtra("timeIn");
ReceiveTimeOut = data.getStringExtra("timeOut");
if (mClickedPosition == -1) { // if icon clicked
if (objMyCustomBaseAdapter != null)
objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);
} else {
if (objMyCustomBaseAdapter != null)
objMyCustomBaseAdapter.changeItem(mClickedPosition, ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);
}
}
}
Edit_Details.java
save.setOnClickListener(new View.OnClickListener() { // return values to previous activity
@Override
public void onClick(View v) {
Toast.makeText(getApplication(), "D", Toast.LENGTH_LONG).show();
Intent returnIntent = new Intent();
project1 = Project2.getSelectedItem().toString();
description = Description.getText().toString();
progress = seekBar.getProgress();
returnIntent.putExtra("project1", project1);
returnIntent.putExtra("description", description);
returnIntent.putExtra("progress", progress);
Toast.makeText(getApplicationContext(), progress + "", Toast.LENGTH_LONG).show();
returnIntent.putExtra("timeIn", timeIn);
returnIntent.putExtra("timeOut", timeOut);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
@Override
public void onBackPressed() {
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
MyCustomBaseAdapter
public class MyCustomBaseAdapter extends BaseAdapter{ // for ListView
private static ArrayList<SearchResults> searchArrayList;
FrameLayout footerLayout;
private LayoutInflater mInflater;
ListView listview;
AbsoluteLayout footer;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results,ListView listview,FrameLayout footerLayout,AbsoluteLayout footer) {
searchArrayList = results;
this.listview=listview;
this.footerLayout=footerLayout;
mInflater = LayoutInflater.from(context);
this.footer=footer;
addOrRemoveFooter();
}
public void addOrRemoveFooter(){
if(searchArrayList.size() == 0 && listview.getFooterViewsCount() > 0){
listview.removeFooterView(footer);
listview.removeFooterView(footerLayout);
Log.e("Search",searchArrayList.size()+"");
}else if(listview.getFooterViewsCount() == 0 && searchArrayList.size()>0){
listview.addFooterView(footer);
listview.addFooterView(footerLayout);
Log.e("Search1",searchArrayList.size()+"");
}
else if(searchArrayList.size()!=0)
{
listview.addFooterView(footer);
listview.addFooterView(footerLayout);
}
}
public int getCount() {
return searchArrayList.size();
}
public String getFistTime() {
SearchResults firstTime = this.searchArrayList.get(0);
return firstTime.getTimeIn();
}
public String getLastTime() {
SearchResults lastTime = this.searchArrayList.get(searchArrayList.size() - 1);
return lastTime.getTimeOut();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public void addNewItem(String P,String D,int Per,String I,String O)
{
SearchResults obj = new SearchResults();
obj.setProject(P);
obj.setProgress(" Progress : " + Per);
obj.setTimeIn(" Time In : " + I);
obj.setTimeOut(" Time Out : " + O);
obj.setDescription(" Work Description : " + D);
searchArrayList.add(obj);
this. notifyDataSetChanged();
addOrRemoveFooter();
}
public void removeItem(int position) {
searchArrayList.remove(position);
addOrRemoveFooter();
this. notifyDataSetChanged();
}
public void changeItem(int m,String P,String D,int Per,String TI,String TO)
{
SearchResults obj = new SearchResults();
obj.setProject(P);
obj.setDescription(" Work Description : " + D);
obj.setProgress(" Progress : " + Per);
obj.setTimeIn(" Time In : " + TI);
obj.setTimeOut(" Time Out : " + TO);
searchArrayList.set(m,obj);
this. notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtProject= (TextView) convertView.findViewById(R.id.ListProject);
holder.txtDescription = (TextView) convertView.findViewById(R.id.ListDescription);
holder.txtProgress = (TextView) convertView.findViewById(R.id.ListProgress);
holder.txtIn=(TextView)convertView.findViewById(R.id.ListTimeIn);
holder.txtOut=(TextView)convertView.findViewById(R.id.ListTimeOut);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtProject.setText(searchArrayList.get(position).getProject());
holder.txtDescription.setText(searchArrayList.get(position).getDescription());
holder.txtProgress.setText(searchArrayList.get(position).getProgress());
holder.txtIn.setText(searchArrayList.get(position).getTimeIn());
holder.txtOut.setText(searchArrayList.get(position).getTimeOut());
return convertView;
}
static class ViewHolder {
TextView txtProject;
TextView txtDescription;
TextView txtProgress;
TextView txtIn;
TextView txtOut;
}
}
LogCat 错误
12-07 09:10:46.143 3000-3000/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.myapplication, PID: 3000
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196609, result=0, data=Intent { }} to activity {com.example.project.myapplication/com.example.project.myapplication.GUI.ActivityB}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.set(ArrayList.java:481)
at com.example.project.myapplication.Adapter.MyCustomBaseAdapter.changeItem(MyCustomBaseAdapter.java:111)
at com.example.project.myapplication.GUI.Edit_WorkDetails.onActivityResult(Edit_WorkDetails.java:138)
这两行显示错误
searchArrayList.set(m,obj);
和objMyCustomBaseAdapter.changeItem(mClickedPosition, ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);
如果按下 Edit_Details 中的保存按钮,则只会调用 changeItem()。但我只想回到以前的活动。任何人都可以帮助我???谢谢
【问题讨论】:
-
可怕的变量名。把一些有意义的东西作为变量名,不去读这段代码。您的问题是,正如堆栈跟踪所说,您正在访问大小为 0 的列表的索引 0。
-
你期望它是什么有意义的变量名?我已将所有变量设置为一个名称,例如
ReceiveProject、ReceiveDescription,而不是a和b。如果您对阅读我的代码不感兴趣并且不愿意提供帮助,您可以走开,无需对我的问题投反对票 -
如果我不愿意提供帮助,我不会阅读您的 cmets。请注意,您是在这里寻求帮助的人。只是建议您如何获得更好的帮助。您的代码不会让任何人轻松帮助您。
P、D、m、Per有什么意义?另请注意,我已经说明了您的问题所在,但看起来您对此不感兴趣。 -
检查mClickedPosition的值不能大于或等于arraylist的大小
-
只是一个通知,请阅读此代码约定指南,我无法阅读您的代码github.com/ribot/android-guidelines/blob/master/…
标签: android android-fragments indexoutofboundsexception