我真的不知道要解决这个问题。
在我的某个活动中,它有一个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 :(得分:2)
检查mClickedPosition的值是否必须大于或等于searchArrayList的大小。
在LogCat中检查此行 引起:java.lang.IndexOutOfBoundsException:索引0无效,大小为0
它表示您的ArrayList为空,并且您正在访问其0索引的对象
OnActivityResult还有一件事就是你必须检查返回给你活动的结果代码
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
if (requestCode == PROJECT_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
ReceiveProject = bundle.getStringExtra("project1");
ReceiveDescription = bundle.getStringExtra("description");
ReceiveProgress = bundle.getIntExtra("progress", 0);
ReceiveTimeIn = bundle.getStringExtra("timeIn");
ReceiveTimeOut = bundle.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);
}
}
}
}
};
答案 1 :(得分:1)
我认为您需要首先检查onActivityResult中的结果代码,如下所示
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
if(resultCode == RESULT_OK)
{
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);
}
}
}
}