我正在尝试编写一个笔记管理应用程序,当我添加一些新笔记时,删除它们然后尝试添加新笔记,该应用程序在 setcontentview() 上崩溃。我添加了日志,可以通过删除第一个editText然后添加一个新的来轻松触发它。
public class Mainpage extends AppCompatActivity {
private LinearLayout MyLayout;
private ScrollView sv;
private List<EditText> et = new ArrayList<EditText>();
private List<EditText> ct = new ArrayList<EditText>();
private List<TextView> tv = new ArrayList<TextView>();
private List<CheckBox> cb = new ArrayList<CheckBox>();
private Button btn;
final Context context = this;
private CountDownTimer timer;
boolean isWaiting;
int last_added;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
last_added = 0;
//btn = (Button) findViewById(R.id.btn);
//btn.setVisibility(View.INVISIBLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_mainpage, menu);
sv = new ScrollView(this);
MyLayout = new LinearLayout(this);
MyLayout.setOrientation(LinearLayout.VERTICAL);
sv.addView(MyLayout);
timer = new CountDownTimer(500,500) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
isWaiting = false;
}
};
btn = new Button(this);
btn.setText("Accept");
btn.setVisibility(View.INVISIBLE);
MyLayout.addView(btn);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
else if (id == R.id.action_add) {
et.add(new EditText(this)) ;
tv.add(new TextView(this));
ct.add(new EditText(this));
cb.add(new CheckBox(this));
cb.get(ct.size() -1).setVisibility(View.INVISIBLE);
ct.get(ct.size() -1).setVisibility(View.INVISIBLE);
Calendar cal = Calendar.getInstance();
String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
tv.get(tv.size() -1).setText("created on" + date);
et.get(et.size() - 1).setText("weeeeeeeeeee~!");
MyLayout.addView(et.get(et.size() - 1));
MyLayout.addView(ct.get(ct.size() -1));
MyLayout.addView(tv.get(tv.size() - 1));
MyLayout.addView(cb.get(cb.size() - 1));
this.setContentView(sv);
et.get(last_added).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<et.size();i++)
{
if(et.get(i) == v)
{
last_added = i + 1;
break;
}
}
if(isWaiting) {
isWaiting = false;
timer.cancel();
// TODO: open a new EditText
if(ct.get(last_added -1).isShown())
{
ct.get(last_added -1).setVisibility(View.INVISIBLE);
}
else
{
ct.get(last_added -1).setVisibility(View.VISIBLE);
}
}
else {
isWaiting = true;
timer.start();
}
}
});
last_added++;
return true;
}
else if (id == R.id.action_delete) {
btn.setVisibility(View.VISIBLE);
for (int i = 0; i < cb.size(); i++) {
cb.get(i).setVisibility(View.VISIBLE);
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int j=0; j < cb.size();j++)
{
if(cb.get(j).isChecked())
{
et.get(j).setVisibility(View.GONE);
tv.get(j).setVisibility(View.GONE);
ct.get(j).setVisibility(View.GONE);
cb.get(j).setVisibility(View.GONE);
et.remove(j);
tv.remove(j);
ct.remove(j);
cb.remove(j);
}
}
btn.setVisibility(View.INVISIBLE);
for (int i = 0; i < cb.size(); i++) {
cb.get(i).setVisibility(View.INVISIBLE);
}
}
});
this.setContentView(sv);
return true;
}
else if (id == R.id.action_search) {
return true;
}
else if (id == R.id.action_send) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
日志
10-16 11:10:34.203 13030-13030/com.example.user_pc.notes E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user_pc.notes, PID: 13030
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.user_pc.notes.Mainpage.onOptionsItemSelected(Mainpage.java:125)
at android.app.Activity.onMenuItemSelected(Activity.java:2908)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:325)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:609)
at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:619)
at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)